Android解决Fragment多层嵌套时onActivityResult无法正确回调的问题

DeaACI 8年前
   <h2><strong>前言:</strong></h2>    <p>Fragment也可以使用startActivityForResult方法去打开一个Activity,然后在其onActivityResult方法中处理结果,可是当Fragment嵌套的时候,由于FragmentActivity的BUG导致只会回调最外那层Fragment的onActivityResult方法,于是乎当前Fragment就收不到结果了。</p>    <h2><strong>BUG分析:</strong></h2>    <p>解决这个问题之前我们先通过源码分析一下是什么原因导致的,以22.2.1版本的support-v4库为例</p>    <p>我们先从Fragment的startActivityForResult开始分析</p>    <pre>  <code class="language-java">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);      }  }</code></pre>    <p>很明显直接调用了FragmentActivity的startActivityForFragment方法</p>    <pre>  <code class="language-java">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'));      }  }</code></pre>    <p>在这里将requestCode和Fragment的mIndex融合成了一个整型作为新的requestCode,那么新的requestCode的高16位表示Fragment的索引,低16为表示原本的requestCode,看来这里是用Fragment的mIndex作为查找依据的</p>    <p>接下来再看FragmentActivity的onActivityResult方法</p>    <pre>  <code class="language-java">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);      }  }</code></pre>    <p>在这里取出requestCode的高16位,不等0就是子Fragment的mIndex,但是接下来却直接从Activity的Fragment列表中根据索引去找Fragment,如果你的Fragment是被子Fragment的childFragmentManager管理的话这样是绝对找不到的,于是乎答案很明了了。</p>    <h2><strong>解决问题:</strong></h2>    <p>解决这个问题的办法有两个</p>    <p>第一个是直接升级support-v4到23.2.0以上的版本,因为23.2.0以上就修复了这个BUG,具体怎么解决的就不再赘述了有兴趣的可自行研究,但由于种种原因没办法升级23.2.0的还大有人在</p>    <p>第二种办法就是自己动手丰衣足食,在Fragment层重写相关方法解决问题</p>    <p>接下来着重介绍自己动手丰衣足食的办法,完整实现如下:</p>    <pre>  <code class="language-java">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) {        }  }</code></pre>    <p>具体思路一句话概括就是startActivityForResult的时候一层一层晚上让父Fragment持有子Fragment的引用,回调onActivityResult的时候父Fragment再一层一层传给子Fragment</p>    <p>具体用法就是让所有的Fragment都继承ForResultNestedCompatFragment,然后用onActivityResultNestedCompat方法替换onActivityResult方法</p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/bee944277e37</p>    <p> </p>