1

I have read from @antonyt's answer to this StackOverflow question that FragmentPagerAdapter will try to reuse an existing fragment found by FragmentManager.findFragmentByTag() upon orientation change.

I have tried this. getItem(int) of the FragmentPagerAdapter is called when the Fragment is first needed. Upon orientation change, getItem is not called, which is in accordance with @antonyt's answer. But why is it that onCreate() of the Fragment is called again? I thought it was not destroyed?

Community
  • 1
  • 1
Jonas
  • 534
  • 8
  • 16

1 Answers1

1

All fragments which not set setRetainInstance(true) are destroyed on orientation change. After orientation changed all fragments are recreated and reattach by fragment manager (so fragment goes throught entire lifecycle).

All fragment attached by ViewPager are added to fragment manager with special tag. ViewPager try to reuse fragment recreated after orientation changed (by searching for tag in fragment manager). If such fragment don't exist then FragmentPagerAdapter.getItem(int) is called to create new fragment.

Kamil W
  • 146
  • 3
  • So will a new Fragment with a new tag be added to the FragmentManager? If a new Fragment instance is to be created, why try to find the "old" one from the FragmentManager? – Jonas Sep 09 '15 at 13:08
  • 1
    Tag of recreated fragment stays this same. ViewPager not trying to find "old" fragment (its been destroyed), but recreated fragment (which has this same tag as the old one). – Kamil W Sep 09 '15 at 13:16