????你回答的是啥,我问的是startActivityForResult()怎么使用
时间: 2024-05-15 17:19:15 浏览: 6
非常抱歉,我理解错了您的问题。下面是startActivityForResult()的使用方法:
startActivityForResult()是用于启动一个新的Activity并等待该Activity的返回结果的方法。当目标Activity完成它的任务并返回结果时,onActivityResult()方法会被调用,并且可以通过该方法获取目标Activity返回的结果。
以下是startActivityForResult()的使用步骤:
1. 在调用Activity的地方使用以下代码启动Activity:
```
Intent intent = new Intent(this, TargetActivity.class);
startActivityForResult(intent, requestCode);
```
其中,TargetActivity是要启动的Activity,requestCode是请求码,用于标识返回的结果。可以在onActivityResult()方法中使用该requestCode来确定哪个Activity返回了结果。
2. 在目标Activity中,通过以下代码设置返回结果:
```
Intent intent = new Intent();
intent.putExtra("result", result);
setResult(resultCode, intent);
finish();
```
其中,result是返回的结果数据,resultCode是返回码,用于标识返回的结果类型。
3. 在调用Activity的地方,重写onActivityResult()方法,处理返回结果:
```
@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("result");
//处理返回结果
}
}
```
在onActivityResult()方法中,可以通过requestCode和resultCode获取返回的结果类型,通过data获取返回的结果数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)