The activity hosting this fragment has its onActivityResult
called when the camera activity returns.
My fragment starts an activity for a result with th
The hosting activity overrides onActivityResult()
, but it did not make a call to super.onActivityResult()
for unhandled result codes. Apparently, even though the fragment is the one making the startActivityForResult()
call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult()
for all unhandled results, the fragment got a shot at handling the result.
And also from @siqing answer:
To get the result in your fragment make sure you call startActivityForResult(intent,111);
instead of getActivity().startActivityForResult(intent,111);
inside your fragment.
Original post.
FragmentActivity
replaces requestCode
by a modified one. After that, when onActivityResult()
will be invoked, FragmentActivity
parses the higher 16 bits and restores the index of the original Fragment. Look at this scheme:
If you have a few fragments at the root level there are no problems. But if you have nested fragments, for example Fragment
with a few tabs inside ViewPager
, you guaranteed will face with a problem (or already faced it).
Because only one index is stored inside requestCode
. That is index of Fragment
inside its FragmentManager
. When we are using nested fragments, there are child FragmentManager
s, which have their own list of Fragments. So, it's necessary to save the whole chain of indices, starting from root FragmentManager
.
How do we resolve this issue? There is common workaround solution in this post.
GitHub: https://github.com/shamanland/nested-fragment-issue
I was also facing the same problem once I shifted this block of code outside of a Fragment to a Utility Class, with parentActivity
passed as argument,
Intent intent = new Intent(parentActivity, CameraCaptureActivity.class);
parentActivity.startActivityForResult(intent,requestCode);
Then I was not getting any value in onActivityResult
method of that Fragment,
Afterwards, I changed the argument to Fragment, so the revised definition of method looked like,
Intent intent = new Intent(fragment.getContext(), CameraCaptureActivity.class);
fragment.startActivityForResult(intent,requestCode);
After that, I was able to get value in onActivityResult
on the Fragment
Kotlin version (In your activity onActivityResult())
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//add following lines in your activity
if(supportFragmentManager?.fragments!=null && supportFragmentManager?.fragments!!.size>0)
for (i in 0..supportFragmentManager?.fragments!!.size-1) {
val fragment= supportFragmentManager?.fragments!!.get(i)
fragment.onActivityResult(requestCode, resultCode, data)
}
}
I can add two advices if someone still cannot make it. In Manifest.xml file, make sure the hosting activity didn't finish when call back and the activity to be started has the launch mode as standard. See details as below:
For Hosting activity, set the no history property as false if have
android:noHistory="false"
For Activity to be started, set the launch mode as standard if have
android:launchMode="standard"
I think you called getActivity().startActivityForResult(intent,111);
. You should call startActivityForResult(intent,111);
.