问题
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:
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;
}
}
来源:https://stackoverflow.com/questions/35476960/pass-onactivityresult-data-to-the-same-fragment-which-is-not-yet-ready