Android Lifecycle management of Fragments within ViewPager and FragmentPagerAdapter

前端 未结 3 798
迷失自我
迷失自我 2020-12-30 05:24

I have been struggling to find out what the correct management of Fragments within a FragmentActivity with a ViewPager is. Before I go into details

3条回答
  •  隐瞒了意图╮
    2020-12-30 05:40

    I believe that this question, about retrieving the current fragment from a ViewPager, will help you. As already pointed out, fragments are managed by the Fragment(State)PagerAdapter and NOT Activity's or Fragment's lifecycle.

    • The first time the activity is created, fragments are returned by the getItem method. This method is called only once per fragment, even if the activity gets recreated.
    • Subsequent times, the fragments are returned by the instantiateItem method. Most probably, this is the place, where you need to get hold of your fragments and call their refresh methods.

提交回复
热议问题