Android的Fragment中onActivityResult不被調用的解決方案(絕對管用)

來源:互聯網
上載者:User

標籤:

常見的,我們會在FragmentActivity中嵌套一層Fragment使用,甚至Fragment下層層嵌套使用。這個時候,在第二級或者更深層級的Fragment將無法收到onActivityResult回調,查看FragementActivity的源碼發現:

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

原來,程式猿偷懶,沒有處理嵌套Fragment的情況,也就是說回調只到第一級Fragment,就沒有繼續分發。我們可以實現一個自己的FragmentActiviy,來實現繼續分發,如下:

public class BaseFragmentActiviy 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;  } } /**  * 遞迴調用,對所有子Fragement生效  *   * @param frag  * @param requestCode  * @param resultCode  * @param data  */ private void handleResult(Fragment frag, int requestCode, int resultCode,   Intent data) {  frag.onActivityResult(requestCode & 0xffff, resultCode, data);  List<Fragment> frags = frag.getChildFragmentManager().getFragments();  if (frags != null) {   for (Fragment f : frags) {    if (f != null)     handleResult(f, requestCode, resultCode, data);   }  } }

然後我們繼承這個BaseFragmentActivity即可,但是要注意,在Fragment中啟動Activity時,一定要調用根Fragment的啟動方法,如下:

/**  * 得到根Fragment  *   * @return  */ private Fragment getRootFragment() {  Fragment fragment = getParentFragment();  while (fragment.getParentFragment() != null) {   fragment = fragment.getParentFragment();  }  return fragment; } /**  * 啟動Activity  */ private void onClickTextViewRemindAdvancetime() {  Intent intent = new Intent();  intent.setClass(getActivity(), YourActivity.class);  intent.putExtra("TAG","TEST");   getRootFragment().startActivityForResult(intent, 1001); }

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android的Fragment中onActivityResult不被調用的解決方案(絕對管用)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.