Expected the adapter to be 'fresh' while restoring state

前端 未结 7 2438
一个人的身影
一个人的身影 2021-02-20 10:24

I have a viewpager2 with multiple fragments in FragmentStateAdapter. Whenever I try to open a new fragment and then go back to my current one with viewpager2, I get an exception

相关标签:
7条回答
  • 2021-02-20 10:44

    So my problem was that I was creating my FragmentStateAdapter inside my Fragment class field where it was only created once. So when my onCreateView got called a second time I got this issue. If I recreate adapter on every onCreateView call, it seems to work.

    0 讨论(0)
  • 2021-02-20 10:48

    it can be fixed by viewPager2.isSaveEnabled = false

    0 讨论(0)
  • 2021-02-20 10:54

    This adapter/view is useful as a replacement for FragmentStatePagerAdapter.

    If what you seek is to preserve the Fragments on re-entrance from the Backstack that would be extremely difficult to achieve with this adapter.

    The team placed to many breaks in place to prevent this, only god knows why...

    They could have used a self detaching lifeCycle observer, which ability was already foresaw in its code, but nowhere in the android architecture makes use of that ability....

    They should have used this unfinished component to listen to the global Fragments lifecycle instead of its viewLifeCycle, from here on, one can scale the listening from the Fragment to the viewLifeCycle. (attach/detach viewLifeCycle observer ON_START/ON_STOP)

    Second... even if this is done, the fact that the viewPager itself is built on top of a recyclerView makes it extremely difficult to handle what you would expect from a Fragment's behavior, which is an state of preservation, a one time instantiation, and a well defined lifecycle (controllable/expected destruction).

    This adapter is contradictory in its functionality, it checks if the viewPager has already been fed with Fragments, while still requiring a "fresh" adapter on reentrance.

    It preserves Fragments on exit to the backStack, while expecting to recreate all of them on reentrance.

    The breaks on place to prevent a field instantiated adapter, assuming all other variables are already accounted for a proper viewLifeCycle handling (registering/unregistering & setting and resetting of parameters) are:

    @Override
        public final void restoreState(@NonNull Parcelable savedState) {
            if (!mSavedStates.isEmpty() || !mFragments.isEmpty()) {
                throw new IllegalStateException(
                        "Expected the adapter to be 'fresh' while restoring state.");
            }
    .....
    }
    

    Second break:

    @CallSuper
    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        checkArgument(mFragmentMaxLifecycleEnforcer == null);
        mFragmentMaxLifecycleEnforcer = new FragmentMaxLifecycleEnforcer();
        mFragmentMaxLifecycleEnforcer.register(recyclerView);
    }
    

    where mFragmentMaxLifecycleEnforcer must be == null on reentrance or it throws an exception in the checkArgument().

    Third: A Fragment garbage collector put in place upon reentrance (to the view, from the backstack) that is postDelayed at 10 seconds that attempts to Destroy off screen Fragments, causing memory leaks on all offscreen pages because it kills their respective FragmentManagers that controls their LifeCycle.

    private void scheduleGracePeriodEnd() {
        final Handler handler = new Handler(Looper.getMainLooper());
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                mIsInGracePeriod = false;
                gcFragments(); // good opportunity to GC
            }
        };
    
        mLifecycle.addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source,
                    @NonNull Lifecycle.Event event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    handler.removeCallbacks(runnable);
                    source.getLifecycle().removeObserver(this);
                }
            }
        });
    
        handler.postDelayed(runnable, GRACE_WINDOW_TIME_MS);
    }
    

    And all of them because of its main culprit: the constructor:

    public FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
            @NonNull Lifecycle lifecycle) {
        mFragmentManager = fragmentManager;
        mLifecycle = lifecycle;
        super.setHasStableIds(true);
    }
    
    0 讨论(0)
  • 2021-02-20 10:57

    I solved this problem by testing if it is equal null

    if(recyclerView.adapter == null) {recyclerView.adapter = myAdapter}
    
    0 讨论(0)
  • 2021-02-20 11:00

    I encountered the same problem with ViewPager2. After a lot of efforts on testing different methods this worked for me:

    public void onExitOfYourFragment() {
        viewPager2.setAdapter(null);
    }
    

    When you come back to the fragment again:

    public void onResumeOfYourFragment() {
        viewPager2.setAdapter(yourAdapter);
    }
    
    0 讨论(0)
  • 2021-02-20 11:02

    I was also getting this java.lang.IllegalStateException: Expected the adapter to be 'fresh' while restoring state. when using ViewPager2 within a Fragment.

    It seems the problem was because I was executing mViewPager2.setAdapter(mFragmentStateAdapter); in my onCreateView() method.

    I fixed it by moving mViewPager2.setAdapter(mMyFragmentStateAdapter); to my onResume() method.

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