android入门:activity之间跳转,并且回传参数
时间: 2024-03-10 16:48:06 浏览: 28
好的,针对你的问题,我可以为你提供以下的解答。
在 Android 中,我们可以通过 Intent 对象来实现 Activity 之间的跳转。下面是一个简单的示例代码,演示了如何从一个 Activity 跳转到另一个 Activity,并且在跳转后传递一些参数:
```
// 在第一个 Activity 中,设置跳转 Intent 并传递参数
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("paramName", paramValue);
startActivityForResult(intent, requestCode);
// 在第二个 Activity 中,获取传递过来的参数并返回结果
Intent resultIntent = new Intent();
resultIntent.putExtra("resultName", resultValue);
setResult(Activity.RESULT_OK, resultIntent);
finish();
```
以上代码中,我们通过 `Intent.putExtra()` 方法在第一个 Activity 中传递了一个参数,并且通过 `startActivityForResult()` 方法启动了第二个 Activity。在第二个 Activity 中,我们可以通过 `getIntent()` 方法获取传递过来的参数,并且在处理完业务逻辑之后,通过 `setResult()` 方法设置返回结果,然后调用 `finish()` 方法关闭当前 Activity 并返回结果。
需要注意的是,我们在启动第二个 Activity 的时候使用了 `startActivityForResult()` 方法而不是 `startActivity()` 方法,这是因为我们需要在第二个 Activity 中返回结果给第一个 Activity。在第一个 Activity 中,我们需要重写 `onActivityResult()` 方法来接收第二个 Activity 返回的结果:
```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == requestCode && resultCode == RESULT_OK) {
String result = data.getStringExtra("resultName");
// 处理返回结果
}
}
```
以上就是 Android 中 Activity 之间跳转并且传递参数的基本方法。希望能够对你有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)