I have a ViewPager
with a FragmentStatePagerAdapter
and the starting page should be somewhere in the middle. But when the user goes to another Activity
and then comes back, the page number should be saved, so that he returns to the same page. There is probably an easy way to get this done, but I can't find the solution.
Usually the current page number will be saved, so this is not the point. The problem is that I don't know where to put the code that sets the starting page at first, because when I put it in onCreate
, the starting page will be shown even if I come back from a different Activity
.
I also tried saving the number in a variable in the onPause
method:
this.currentPageNumber = viewPager.getCurrentItem();
But next time when onStart()
is called, the variable currentPageNumber
is null
, so it seems that variables are not saved after onDestroy()
.
I could use a static variable, but it feels wrong. Is there a better solution?
EDIT: Sorry, I didn't make clear enough, that I want the saved page only be opened, if I come back to this Activity after I launched it. Every time I start the Activity
from the launcher, the starting page should be shown and not the saved page.
EDIT 2: The behaviour I want to have is exactly the same as the Google Calendar app has when you open the day or week perspective. If I open this app, the current day will be shown. If I swipe to another day, then open settings, then press back, this other day is still be shown. But if I restart the app, again today will be shown.