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的区别;

相关推荐
Propeller31 分钟前
【Android】View 交互的事件处理机制
android·java
吴Wu涛涛涛涛涛Tao33 分钟前
Flutter 实现「可拖拽评论面板 + 回复输入框 + @高亮」的完整方案
android·flutter·ios
雨声不在37 分钟前
使用android studio分析cpu开销
android·ide·android studio
程序leo源2 小时前
Linux_基础指令(二)
android·linux·运维·服务器·青少年编程
雨白4 小时前
Android 两种拖拽 API 详解:ViewDragHelper 和 OnDragListener 的原理与区别
android
元亓亓亓4 小时前
JavaWeb--day3--Ajax&Element&路由&打包部署
android·ajax·okhttp
居然是阿宋4 小时前
Android XML属性与Jetpack Compose的对应关系(控件基础属性篇)
android
GoatJun5 小时前
Android ScrollView嵌套RecyclerView 导致RecyclerView数据展示不全问题
android
潜龙95275 小时前
第6.2节 Android Agent开发<二>
android·python·覆盖率数据