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

相关推荐
2501_915921431 小时前
iOS 虚拟位置设置实战,多工具协同打造精准调试与场景模拟环境
android·ios·小程序·https·uni-app·iphone·webview
龚礼鹏1 小时前
Android 图像显示框架三——演示demo以及解析
android·交互
QuantumLeap丶1 小时前
《Flutter全栈开发实战指南:从零到高级》- 11 -状态管理Provider
android·flutter·ios
百锦再1 小时前
第6章 结构体与方法
android·java·c++·python·rust·go
gustt2 小时前
用小程序搭建博客首页:从数据驱动到界面展示
android·前端·微信小程序
金鸿客2 小时前
Compose从相册和系统相机拍照获取照片
android
IT乐手2 小时前
Android 获取定位信息工具类
android
yangjunjin3 小时前
Android ANR的解决方案
android
低调小一3 小时前
Android Gradle 的 compileOptions 与 Kotlin jvmTarget 全面理解(含案例)
android·开发语言·kotlin
苦学编程啊7 小时前
【2025Flutter 入门指南】Dart SDK 安装与 VS Code 环境配置-Windows
android·dart