Please note, this question is not a duplicate of the following:
- https://stackoverflow.com/questions/19006776/onactivityresult-not-working-with-fragments
- onActivityResult not working on fragments
Also, another similar question was asked before, but that doesn't mention orientation changes (and is unresolved).
My onActivityResult
method in the Fragment
does get called if I don't switch orientation. However, if I follow these steps, it doesn't get called:
- Load fragment in FragmentActivity.
- From the fragment:
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), Constants.REQ_CODE_IMAGE_CAPTURE);
- Wait for the camera to load.
- Switch orientation.
- Take picture and click the checkmark.
onActivityResult
still gets called in the parent FragmentActivity. However, due to this warning I am getting:
W/FragmentActivity(4418): Activity result no fragment exists for index: 0x22d73
...my guess is that the parent gets destroyed due to the orientation change, and after having been re-created, can't find the Fragment
that called startActivityForResult
in the first place.
Is this a framework bug? How can this be worked around?
EDIT: Added more code due to popular demand.
FragmentActivity.java:
...
fragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace (R.id.mainContentView, fragment);
if (clearBackStack) {
// clear the back stack
while (getSupportFragmentManager().popBackStackImmediate ());
// add the current transaction to the back stack
transaction.addToBackStack (null);
}
else {
transaction.addToBackStack(null);
}
transaction.commit();
...
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
}
ExampleFragment.java:
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, Constants.REQ_CODE_IMAGE_CAPTURE);
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case Constants.REQ_CODE_IMAGE_CAPTURE:
// handle added image
}
}
Resolved it by moving all of the
onActivityResult
logic from theFragment
to theActivity
.Do note, however, that the
reqCode
that gets passed to theFragment
is different than thereqCode
that gets delivered to the parentActivity
(the one for theActivity
is set by the system I think). This meant that I had to also move the originalstartActivityForResult
call from theFragment
to theActivity
, and then create a method to call it from theFragment
when needed.If anyone finds a more elegant way, I would be happy to hear about it.
i Have one Solution it is currently Working in my project. follow Step.
--> Create Interface name OnActivityResultListener(or name as you wish).
public interface OnActivityResultListener {
}
--> Make refrence of intercace in your FragmentActivity like
--> now override OnActivityResult in FragmentActivity. like below.
--> now implement interface in your Fragment and override required method.
--> and the last step is inside OnAttach Method of Fragment class is register your Fragment in activity for the the Activity result. like
-->now test and don't forgot to remove it
->it is some what lengthy process but it is working 100% in any orientation.