Solution in Android Fragment where onActivityResult is not called (absolutely useful)

Source: Internet
Author: User

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 );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.