Pass onActivityResult() data to the same Fragment

2019-07-17 15:11发布

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.

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.

1条回答
淡お忘
2楼-- · 2019-07-17 15:45

Try 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;
    }
}
查看更多
登录 后发表回答