前言:
Fragment也可以使用startActivityForResult方法去開啟一個Activity,然後在其onActivityResult方法中處理結果,可是當Fragment巢狀的時候,由於FragmentActivity的BUG導致只會回撥最外那層Fragment的onActivityResult方法,於是乎當前Fragment就收不到結果了。
BUG分析:
解決這個問題之前我們先通過原始碼分析一下是什麼原因導致的,以22.2.1版本的support-v4庫為例
我們先從Fragment的startActivityForResult開始分析
public void startActivityForResult(Intent intent, int requestCode) {
if(this.mActivity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to Activity");
} else {
this.mActivity.startActivityFromFragment(this, intent, requestCode);
}
}
複製程式碼
很明顯直接呼叫了FragmentActivity的startActivityFromFragment方法
public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode) {
if(requestCode == -1) {
super.startActivityForResult(intent, -1);
} else if((requestCode & -65536) != 0) {
throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
} else {
super.startActivityForResult(intent, (fragment.mIndex + 1 << 16) + (requestCode & '\uffff'));
}
}
複製程式碼
在這裡將requestCode和Fragment的mIndex融合成了一個整型作為新的requestCode,那麼新的requestCode的高16位表示Fragment的索引,低16為表示原本的requestCode,看來這裡是用Fragment的mIndex作為查詢依據的
接下來再看FragmentActivity的onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
this.mFragments.noteStateNotSaved();
int index = requestCode >> 16;
if(index != 0) {
--index;
if(this.mFragments.mActive != null && index >= 0 && index < this.mFragments.mActive.size()) {
Fragment frag = (Fragment)this.mFragments.mActive.get(index);
if(frag == null) {
Log.w("FragmentActivity", "Activity result no fragment exists for index: 0x" + Integer.toHexString(requestCode));
} else {
frag.onActivityResult(requestCode & '\uffff', resultCode, data);
}
} else {
Log.w("FragmentActivity", "Activity result fragment index out of range: 0x" + Integer.toHexString(requestCode));
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
複製程式碼
在這裡取出requestCode的高16位,不等0就是子Fragment的mIndex,但是接下來卻直接從Activity的Fragment列表中根據索引去找Fragment,如果你的Fragment是被子Fragment的childFragmentManager管理的話這樣是絕對找不到的,於是乎答案很明瞭了。
解決問題:
解決這個問題的辦法有兩個 第一個是直接升級support-v4到23.2.0以上的版本,因為23.2.0以上就修復了這個BUG,具體怎麼解決的就不再贅述了有興趣的可自行研究,但由於種種原因沒辦法升級23.2.0的還大有人在 第二種辦法就是自己動手豐衣足食,在Fragment層重寫相關方法解決問題
接下來著重介紹自己動手豐衣足食的辦法,完整實現如下:
public class ForResultNestedCompatFragment extends Fragment {
private ForResultNestedCompatFragment forResultChildFragment;
@Override
public void startActivityForResult(Intent intent, int requestCode) {
Fragment parentFragment = getParentFragment();
if (parentFragment != null && parentFragment instanceof ForResultNestedCompatFragment) {
((ForResultNestedCompatFragment) parentFragment).startActivityForResultFromChildFragment(intent, requestCode, this);
} else {
forResultChildFragment = null;
super.startActivityForResult(intent, requestCode);
}
}
private void startActivityForResultFromChildFragment(Intent intent, int requestCode, ForResultNestedCompatFragment childFragment) {
forResultChildFragment = childFragment;
Fragment parentFragment = getParentFragment();
if (parentFragment != null && parentFragment instanceof ForResultNestedCompatFragment) {
((ForResultNestedCompatFragment) parentFragment).startActivityForResultFromChildFragment(intent, requestCode, this);
} else {
super.startActivityForResult(intent, requestCode);
}
}
@Override
public final void onActivityResult(int requestCode, int resultCode, Intent data) {
if (forResultChildFragment != null) {
forResultChildFragment.onActivityResult(requestCode, resultCode, data);
forResultChildFragment = null;
} else {
onActivityResultNestedCompat(requestCode, resultCode, data);
}
}
public void onActivityResultNestedCompat(int requestCode, int resultCode, Intent data) {
}
}
複製程式碼
具體思路一句話概括就是startActivityForResult的時候一層一層往上讓父Fragment持有子Fragment的引用,回撥onActivityResult的時候父Fragment再一層一層傳給子Fragment
具體用法就是讓所有的Fragment都繼承ForResultNestedCompatFragment,然後用onActivityResultNestedCompat方法替換onActivityResult方法
如果你覺得有用,就點個喜歡吧!