IllegalStateException: is not currently in the FragmentManager

后端 未结 12 2112
予麋鹿
予麋鹿 2021-01-31 08:40

I know it sounds like a duplicate of FragmentStatePagerAdapter IllegalStateException: is not currently in the FragmentManager but his solution isn\'t relevan

相关标签:
12条回答
  • 2021-01-31 09:13

    Use getChildFragmentManager() instead of getFragmentManager() if you have fragments hierarchy. eg. if you have fragments pager.

    0 讨论(0)
  • 2021-01-31 09:14

    Are you calling FragmentStatePagerAdapter.instantiateItem() from your own code? Generally, the ViewPager should be the only client that calls this method, however, it's easy to come across workarounds for other limitations that rely on this method.

    Due to the way FragmentStatePagerAdapter works, I would guess that the adapter has instantiated the Fragment, added it to it's internal collection of Fragments, and even began a transaction to add that Fragment to the FragmentManager, but that fragment transaction has not been committed or executed. This is exactly what instantiateItem() does when it hasn't already instantiated the requested Fragment.

    Another method, FragmentStatePagerAdapter.finishUpdate() is responsible for committing the transaction as well as for executing the pending transactions. finishUpdate() must be called after instantiateItem() or else the Fragment may not be added to the FragmentManager.

    Try something like this:

    // Get the current Fragment presented by the ViewPager.
    pagerAdapter.startUpdate(viewPager);
    Fragment fragment = pagerAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());
    pagerAdapter.finishUpdate(viewPager);
    return fragment;
    

    startUpdate() has an empty implementation as of API level 19.

    0 讨论(0)
  • 2021-01-31 09:15

    This can happen when a FragmentStatePagerAdapter is set on a ViewPager, with the Fragments being populated, but not yet added to the FragmentManager due to the Activity pausing before layout has occurred for the ViewPager. This is reliably hit by starting another Activity from onCreate, with the Adapter also set in onCreate. In that situation, it's best to hold off on setting the Adapter, and set it onActivityResult instead. See this issue: https://code.google.com/p/android/issues/detail?id=77285

    I've also submitted a patch to check for the Fragment having been added before trying to save state.

    0 讨论(0)
  • 2021-01-31 09:30

    If your ViewPager is layouted inside a fragment (not an activty) :

    mViewPager.setAdapter(new MyFragmentStatePagerAdapter(getChildFragmentManager()));

    0 讨论(0)
  • 2021-01-31 09:30

    Fragments in the ViewPager are fixed, instead of trying to replace the fragments in the adapter, try to give a different set of fragments and notifyDataSet changed, or take the advantage of FrameLayout to show another fragment over the view pager tab's current fragment.

    There is my solution that works:

    Swipe Gesture applied at Fragment level along with ViewPager with it's default swipe disabled

    0 讨论(0)
  • 2021-01-31 09:32

    I've encountered the exact exception.

    In my case, I have several fragments managed by FragmentPagerStateAdapter, but sometimes the fragment will get switched in position.

    I override getItemPosition() and return the new position and call notifyDataSetChanged() to refresh ViewPager. Everything works fine but sometimes when I leave the ViewPager the crash occurs.

    After several hours digging into it, I found there's a bug in FragmentPagerStateAdapter.

    The adapter not only caches states, but also caches the fragments that it consider 'active'

    private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    

    But it does not check if the fragments' position has invalidated or moved.

    That's how it get this exception:

    1.I have A,B,C three Fragments in ViewPager and Adapter.

    2.I switched the position to B,A,C in adapter and called notifyDataSetChanged().

    3.ViewPager re-layout the fragment by new order. But the mFragments cache is still {A,B,C}

    4.Swipe few pages, ViewPager will ask adapter to destroy the first Fragment. then the fragment B, instead of A, is set to null in the cache. Now the mFragments is {null,A,C}.

    5.Leave the ViewPager, and onSaveInstanceState is triggered. all fragments in mFragments is considered active and putFragment() is called until FragmentManagerImpl found A not in it and throw a Exception.

    So here's my not so gentle solution:

    1.Copy the entire FragmentPagerStateAdapter source code.

    2.Override the notifyDataSetChanged() method to rearrange the caches as below.

    @Override
    public void notifyDataSetChanged() {
      List<Fragment> oldFragments = new ArrayList<>(mFragments);
      List<Fragment.SavedState> oldStates = new ArrayList<>(mSavedState);
      for (int i = 0; i < getCount(); i++) {
        if (i < mFragments.size()) {
          Fragment f = mFragments.get(i);
          if (f != null) {
            int newPosition = getItemPosition(f);
            if (newPosition == POSITION_UNCHANGED || newPosition == i) {
            } else if (newPosition == POSITION_NONE) {
              if (i < mSavedState.size()) {
                mSavedState.set(i, null);
              }
              mFragments.set(i, null);
            } else {
              while (i >= mFragments.size()) {
                mFragments.add(null);
              }
              if (oldStates.size() > i) {
                mSavedState.set(newPosition, oldStates.get(i));
              }
              mFragments.set(newPosition, oldFragments.get(i));
            }
          } else {
            /*
             * No Fragment but that's possible there's savedState and position has
             * changed.
             */
          }
        }
      }
      super.notifyDataSetChanged();
    }
    

    Hope that works for some of you!

    0 讨论(0)
提交回复
热议问题