将onActivityResult()数据传递给尚未准备好的相同片段

时间:2016-02-18 09:03:40

标签: android android-lifecycle fragment-lifecycle

我正在使用Fragment使用Activity开始新startActivityForResult(),我在(Bundle)()方法中获得结果onActivityResult。{在onActivityResult()之前调用了{1}}()。我想确保正确保存/保存onResume,以便在Bundle Fragment's被调用时,我获取保存/保存的结果以执行进一步操作。

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

1 个答案:

答案 0 :(得分:2)

试试这个

@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;
    }
}