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

相关推荐
doublelixin2 小时前
AOSP (Android11) 集成Google GMS三件套
android
xzkyd outpaper5 小时前
onSaveInstanceState() 和 ViewModel 在数据保存能力差异
android·计算机八股
CYRUS STUDIO6 小时前
FART 脱壳某大厂 App + CodeItem 修复 dex + 反编译还原源码
android·安全·逆向·app加固·fart·脱壳
WAsbry7 小时前
现代 Android 开发自定义主题实战指南
android·kotlin·material design
xzkyd outpaper7 小时前
Android动态广播注册收发原理
android·计算机八股
唐墨1237 小时前
android与Qt类比
android·开发语言·qt
林林要一直努力8 小时前
Android Studio 向模拟器手机添加照片、视频、音乐
android·智能手机·android studio
AD钙奶-lalala8 小时前
Mac版本Android Studio配置LeetCode插件
android·ide·android studio
散人10249 小时前
Android Test3 获取的ANDROID_ID值不同
android·unit testing
雨白10 小时前
实现动态加载布局
android