I know there are many posts on why onActivityResult() is not called in the Fragment when startActivityForResult() is called from fragment ; but my case is different.
My activity A has a fragment F which starts an activity B for result. The fragment receives the result no problem here. The problem is that when activity A is restarted, it builds its entire view asynchronously using an AsynTask which is called during onResume(). As a result, my fragment F is covered by Activity A.
In my fragment, I start the Activity like this :
startActivityForResult(intent, NewElementFragment.CODE_IMAGE_PATH);
So I thought, no worries, I know that onResume() is called after onActivityResult(), so all I need to do is make sure in onActivityResult() that the view will not be created during onResume().
I created a boolean that I set to false during onActivityResult() like this :
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//put the NewElementFragment back on top
if (requestCode == NewElementFragment.CODE_IMAGE_PATH){
//we set loadinfo to false so that the activity view won't get built
this.loadInfo = false;
Log.d("MainActivity","the loadInfo boolean was set to false");
}
//Do default action on result (nothing) so that the result is passed on the the fragments
super.onActivityResult(requestCode, resultCode, data);
}
But it did not work because apparently the onActivityResult() was never triggered in my activity A (even though as I said before it is perfectly triggered in my fragment).
So in the end I checked in on Resume if I had an active fragment and if yes, abort the view creation like this :
protected void onResume(){
super.onResume();
/*
* We check if there is a fragment built that should be visible. If yes, we don't build the view
*/
NewElementFragment f = (NewElementFragment) getFragmentManager().findFragmentByTag(TAG_NEW_ELEMENT_FRAGMENT);
if (f!=null){
loadInfo = false;
}
//We call the GetProductDetailTask
if (loadInfo) new GetProductDetailTask(pId).execute();
}
It works but I find it quite ugly. Could someone tell me why the onActivityResult() is never called in my activity A ?
Or if someone has a better solution to make sure that my fragment is not covered by my activity when it is restarted ?
Thank you.
Your hosting activity must implement some Interface, and you must call it from your fragment. In this Interface realization you can call another Activity via startActivityForResult()
. In this case you'll success in onActivityResult()
processing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.