intent 传参调用activity
时间: 2025-06-29 17:14:56 浏览: 11
### 使用 `Intent` 传递参数并启动 Activity
在 Android 开发中,可以通过 `Intent` 对象不仅实现活动之间的导航,还能携带必要的数据。下面展示了一个具体的例子,说明如何创建带有额外参数的 `Intent` 并用于启动新的 Activity。
#### 创建带参数的 Intent 实例
当需要向目标 Activity 发送特定的数据时,可以利用 `putExtra()` 方法给 `Intent` 添加键值对形式的附加信息:
```java
// 构建一个显式的 Intent 来指定要启动的目标 Activity 类型以及发送者上下文
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
// 向 Intent 中加入字符串类型的额外参数 "message"
intent.putExtra("key_message", "Hello from CurrentActivity");
// 如果还有其他类型的数据也可以继续添加
intent.putExtra("another_key", value);
```
#### 在接收端获取传递过来的信息
为了读取这些被传递来的参数,在目标 Activity 的 `onCreate(Bundle savedInstanceState)` 方法内可以从传入的 `Intent` 获取到之前设置好的键对应的值:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 初始化界面布局...
// 从启动此 Activity 的 Intent 取得 Bundle 数据包
Bundle extras = getIntent().getExtras();
if (extras != null){
String message = extras.getString("key_message");
// 处理接收到的消息或其他数据...
}
}
```
上述过程展示了完整的通过 `Intent` 进行跨 Activity 参数传输的方式[^1]。
阅读全文
相关推荐




















