marczych
marczych

Reputation: 967

How to save Fragment state across orientation changes using FragmentStatePagerAdapter?

I'm using a ViewPager with a FragmentStatePagerAdapter and I'm having problems saving the Fragment's state across orientation changes. It works fine when paging back and forth e.g. viewing a page, swiping two pages away, and then going back 2 pages to the original Fragment correctly saves and restores state. I am doing this using onSaveInstanceState and restoring state in onCreateView if the Bundle isn't null.

Changing orientations, however, doesn't work through the same mechanism and from my testing doesn't even call the fragment's onSaveInstanceState method.

Is this expected? Am I missing something to force it to save instance state? Did I do something to stop it from working?

Thanks!

Upvotes: 2

Views: 2496

Answers (1)

marczych
marczych

Reputation: 967

Turns out I wasn't calling super.onSaveInstanceState() in the Activity so none of the ViewPager/Fragment state was maintained. Adding in super.onSaveInstanceState() fixed it!

Upvotes: 1

Related Questions