AndroidActivityResultLauncherapi中如何处理请求代码?
创始人
2024-10-06 05:32:05
0

在Android中,可以使用ActivityResultLauncher api通过Activity/Fragment获取结果。要使用ActivityResultLauncher api,需要创建一个ActivityResultContract实例,并使用registerForActivityResult方法注册它。

在注册ActivityResultLauncher时,需要传入requestCode。requestCode是一个仅由您的应用程序使用的整数,并用于标识发送和返回结果的请求。不同的请求应该使用不同的requestCode。

例如,下面是如何处理请求代码的示例:

public class MainActivity extends AppCompatActivity {

    private final ActivityResultLauncher someActivityResultLauncher =
            registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                    new ActivityResultCallback() {
                        @Override
                        public void onActivityResult(ActivityResult result) {
                            if (result.getResultCode() == RESULT_OK) {
                                Intent data = result.getData();
                                // Handle the Intent
                            }
                        }
                    });
    
    private void launchSomeActivity() {
        Intent intent = new Intent(this, SomeActivity.class);
        someActivityResultLauncher.launch(intent);
    }
}

在上面的示例中,我们注册了一个具有StartActivityForResult行为的ActivityResultContract,并为其传递了一个ActivityResultCallback回调函数。在回调函数中,我们检查返回结果的resultCode是否等于RESULT_OK,并获取返回的Intent数据来处理它。

在启动活动时,我们使用someActivityResultLauncher.launch(intent)方法作为请求代码。在回调函数中,我们可以使用我们自己的逻辑来处理该请求代码对应的活动。

总而言之,处理请求代码的最佳实践是为每个请求代码使用唯一的整数资源,并在Activity/Fragment返回结果时使用它们。

相关内容

热门资讯

推荐一款!wepoker透视苹... 1、推荐一款!wepoker透视苹果系统,悦扑克脚本,教你攻略(有挂软件)。2、wepoker透视苹...
实测分享!hhpoker可以控... 实测分享!hhpoker可以控制吗,aapoker如何设置胜率,必赢方法(有挂攻略);支持多人共享记...
揭秘!we poker辅助器,... 揭秘!we poker辅助器,hhpoker哪个俱乐部靠谱,透视教程(有挂方法)1、许多玩家不知道h...
科普分享!aapoker辅助器... 科普分享!aapoker辅助器怎么用,德州局透视脚本免费版下载手机版,微扑克教程(有挂教程);支持2...
今日公布!hhpoker怎么破... 今日公布!hhpoker怎么破解,wepoker透视脚本是什么,规律教程(有挂神器);1.wepok...
交流学习经验!wepoker插... 1、交流学习经验!wepoker插件辅助,wepoker一直输的号能继续打吗,技巧教程(有挂软件)2...
实测交流!hhpoker是真的... 实测交流!hhpoker是真的还是假的,菠萝辅助器免费版的特点,安装教程(有挂方法)科技教程也叫必备...
玩家必看科普!德普之星app安... 此外,数据分析德州(德普之星app安卓版破解版)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
记者揭秘!aapoker发牌逻... 记者揭秘!aapoker发牌逻辑,哈糖大菠萝软件下载,可靠技巧(有挂透视);1分钟了解详细教程(微信...
大家学习交流!wepoker线... 大家学习交流!wepoker线上大神,wpk私人辅助,科技教程(有挂方法)1、点击下载安装,微扑克w...