Solution in Android Fragment where onActivityResult is not called (absolutely useful)
Generally, Fragment is nested in FragmentActivity, or even in Fragment. At this time, the second-level or deeper Fragment will not be able to receive the onActivityResult callback. Check the source code of FragementActivity and find:
public void startActivityFromFragment(Fragment fragment, Intent intent, : int requestCode) { : if (requestCode == -1) { : super.startActivityForResult(intent, -1); : return; : } : if ((requestCode&0xffff0000) != 0) { : throw new IllegalArgumentException("Can only use lower 16 bits for requestCode"); : } : super.startActivityForResult(intent, ((fragment.mIndex+1)<<16) + (requestCode&0xffff)); : } : @Override : protected void onActivityResult(int requestCode, int resultCode, Intent data) { : mFragments.noteStateNotSaved(); : int index = requestCode>>16; : if (index != 0) { : index--; : if (mFragments.mActive == null || index < 0 || index >= mFragments.mActive.size()) { : Log.w(TAG, "Activity result fragment index out of range: 0x" : + Integer.toHexString(requestCode)); : return; : } : Fragment frag = mFragments.mActive.get(index); : if (frag == null) { : Log.w(TAG, "Activity result no fragment exists for index: 0x" : + Integer.toHexString(requestCode)); : } else { : frag.onActivityResult(requestCode&0xffff, resultCode, data); : } : return; : } : : super.onActivityResult(requestCode, resultCode, data); : } :
Originally, the program was lazy and did not process nested Fragment. That is to say, the callback was only to the first level of Fragment, and the distribution was not continued. We can implement our own FragmentActiviy to achieve continuous distribution, as shown below:
Public class extends FragmentActivity {private static final String TAG = "BaseActivity"; @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {FragmentManager fm = getSupportFragmentManager (); int index = requestCode> 16; if (index! = 0) {index --; if (fm. getFragments () = null | index <0 | index> = fm. getFragments (). size () {Log. w (TAG, "Activity result fragment index out of range: 0x" + Integer. toHexString (requestCode); return;} Fragment frag = fm. getFragments (). get (index); if (frag = null) {Log. w (TAG, "Activity result no fragment exists for index: 0x" + Integer. toHexString (requestCode);} else {handleResult (frag, requestCode, resultCode, data);} return ;}}/*** recursive call, apply to all sub-Fragement ** @ param frag * @ param requestCode * @ param resultCode * @ param data */private void handleResult (Fragment frag, int requestCode, int resultCode, Intent data) {frag. onActivityResult (requestCode & 0 xffff, resultCode, data); List
Frags = frag. getChildFragmentManager (). getFragments (); if (frags! = Null) {for (Fragment f: frags) {if (f! = Null) handleResult (f, requestCode, resultCode, data );}}}
Then we can inherit this BaseFragmentActivity, but note that when starting the Activity in Fragment, you must call the start method of the root Fragment, as shown below:
/*** Get the root Fragment ** @ return */private Fragment getRootFragment () {Fragment fragment = getParentFragment (); while (fragment. getParentFragment ()! = Null) {fragment = fragment. getParentFragment ();} return fragment;}/*** start Activity */private void onClickTextViewRemindAdvancetime () {Intent intent Intent = new intent (); Intent. setClass (getActivity (), YourActivity. class); intent. putExtra ("TAG", "TEST"); getRootFragment (). startActivityForResult (intent, 1001 );}