FragmentActivity onSaveInstanceState not getting called

前端 未结 3 1622
醉话见心
醉话见心 2020-12-05 02:49

I have seen a few similar questions about onSaveInstanceState not getting called for Fragments, but in my case Fragments work fine, it

3条回答
  •  无人及你
    2020-12-05 03:45

    In an update to the accepted answer:

    A fragment's onSaveInstanceState may be called if you are using a ViewPager with a FragmentStatePagerAdapter (rather than FragmentPagerAdapter)

    FragmentStatePagerAdapter

    This version of the pager is more useful when there are a large number of pages, working more like a list view. When pages are not visible to the user, their entire fragment may be destroyed, only keeping the saved state of that fragment. This allows the pager to hold on to much less memory associated with each visited page as compared to FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

    And don't forget:

    When using FragmentPagerAdapter the host ViewPager must have a valid ID set.

提交回复
热议问题