将onActivityResult()数据传递到尚未准备好的同一Fragment [英] Pass onActivityResult() data to the same Fragment which is not yet ready

查看:270
本文介绍了将onActivityResult()数据传递到尚未准备好的同一Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fragment使用startActivityForResult()启动新的Activity,我在onActivityResult()方法中获得了结果(Bundle).由于onActivityResult()在onResume(之前被调用).我想确保正确保存/保存Bundle,以便在调用Fragment's onResume()时获得保存/保存的结果以执行进一步的操作.

I am using a Fragment to start a new Activity using startActivityForResult(), I am getting the result (Bundle) in onActivityResult() method.Since onActivityResult() called before onResume().I want to make sure, I keep/save the Bundle properly so that when Fragment's onResume() gets called, I get the kept/saved result to perform further action.

有哪些不同的方法可以实现这一目标.我尝试使用getArguments()/setArguments(),但这似乎不是实现此目的的正确方法.

What are the different ways to achieve this. I tried using getArguments()/setArguments(), but that seems to be not the right way to achieve this.

推荐答案

尝试一下

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            mResultBundle = data.getExtras(); //mResultBundle is in fragment 
            //scope
        }
    }
}

@Override
protected void onResume(){
    super.onResume();
    if(mResultBundle != null){
       // process saved bundle from activity result here

       // don't forget to set it back to null once you are done
       mResultBundle = null;
    }
}

这篇关于将onActivityResult()数据传递到尚未准备好的同一Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆