Android Fragment中使用Arouter跳转到Activity后返回Fragment不回调onActivityResult

Fragment中通过路由跳转到Activity

  • 跳转传递参数

    通过Arouter跳转

    复制代码
    Postcard postcard = ARouter.getInstance().build(RouterConstant.ACTION_TRANSMANAGERACTIVITY1);
    Bundle bundle = new Bundle();
    bundle.putInt("code", 404);
    postcard.with(bundle); //设置bundle
    fragmentNavigation(postcard,10000);
    
    
    private void fragmentNavigation(Postcard postcard , int requestCode) { 
        LogisticsCenter.completion(postcard);
        Intent intent = new Intent(getActivity(),postcard.getDestination());
        intent.putExtras(postcard.getExtras());
        startActivityForResult(intent, requestCode);
    }
  • Activity中获取参数

    复制代码
      ARouter.getInstance().inject(this);
      num =  getIntent().getExtras().getInt("code");

注意传递的参数Bundle也可以如下传递Bundle参数

设置Bundle参数

复制代码
postcard.withBundle("Bundle",bundle);

Activity中获取Bundle参数

复制代码
getIntent().getBundleExtra("Bundle").getInt("code");

在Activity中返回时别忘记了设置结果或者参数:

复制代码
setResult(Activity.RESULT_OK);

Fragment总就可以重写onActivityResult方法,在此方法获取上一个Activity返回来的数据了或者处理相关业务逻辑了;

注意onActivityResult方法中int requestCode, int resultCode的区别;

相关推荐
XiaoLeisj35 分钟前
Android 权限管理实战:运行时申请、ActivityResultLauncher 与设置页授权
android·java·权限
橙子1991101641 分钟前
Android 中的权限申请
android
2501_915921431 小时前
iOS APP上架工具,在没有 Mac 的环境中发布苹果应用
android·macos·ios·小程序·uni-app·iphone·webview
范特西林1 小时前
第一篇:从电源键到上帝进程——硬件觉醒与 Init 的诞生
android
养了一只皮卡丘1 小时前
ubuntu22.04搭建mysql8.0.45 mgr (2)
android·adb
常利兵1 小时前
深入理解Android ViewModel&SavedStateHandle:告别数据丢失,打造稳健UI架构
android·ui·架构
范特西林1 小时前
第四篇:从点击到显示——App 启动与 Activity 生命周期全追踪
android
ke_csdn1 小时前
安卓的视频通讯
android·音视频
范特西林1 小时前
第二篇:Java 世界的“创世神”:Zygote 如何一秒孵化一个 App?
android
范特西林1 小时前
第三篇:SystemServer——Android 框架层的大脑
android