Android sudio中startActivityForResult(intent, REQUEST_CODE_CHOOSE_CC);怎么用registerForActivityResult方法来写
时间: 2024-05-13 17:20:00 浏览: 19
在 Android Studio 中,可以使用 `registerForActivityResult` 方法来替代 `startActivityForResult` 方法。
以下是将 `startActivityForResult` 方法替换为 `registerForActivityResult` 方法的示例代码:
首先,定义一个 `ActivityResultLauncher` 对象:
```
private ActivityResultLauncher<Intent> mGetContentLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// 处理返回结果
}
});
```
然后在需要启动 Activity 的地方调用 `mGetContentLauncher.launch(intent)` 方法:
```
Intent intent = new Intent(this, YourActivity.class);
mGetContentLauncher.launch(intent);
```
其中,`YourActivity` 是你需要启动的 Activity,而 `mGetContentLauncher` 就是你定义的 `ActivityResultLauncher` 对象。当 Activity 返回结果时,会调用 `ActivityResultCallback` 中的方法,你可以在其中处理返回结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)