Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android的Fragment中onActivityResult不被調用的解決方案(絕對管用)

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

編輯:關於Android編程

常見的,我們會在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 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);
 }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved