安卓registerForActivityResult使用详情代码
时间: 2024-05-03 15:20:16 浏览: 16
好的,关于安卓的 registerForActivityResult 的使用详情代码,以下是一个示例:
在 Activity 或 Fragment 中:
```java
// 定义一个 ActivityResultLauncher
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// 处理返回结果
Intent data = result.getData();
// ...
}
});
// 调用 ActivityResultLauncher 启动另一个 Activity
Intent intent = new Intent(this, AnotherActivity.class);
mLauncher.launch(intent);
```
在 Activity 或 Fragment 中,可以通过定义 `ActivityResultCallback` 接口的实现类来处理 `ActivityResultLauncher` 的返回结果。使用 `ActivityResultLauncher` 的主要过程是:
- 定义一个 `ActivityResultLauncher`
- 调用 `ActivityResultLauncher` 启动另一个 `Activity`
- 处理返回结果
以上是使用 registerForActivityResult 函数的示例代码,您可以根据自己的实际情况进行修改。希望对您有帮助。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)