Reputation: 173
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.
Upvotes: 0
Views: 2710
Reputation: 91
I have the same requirement to show the default page whenever the onCreate
of the ViewPager
hosting Activity
is called and show the current page for all the other cases including the motivating case: when user navigate back from the Activity
started from the current page (onCreate
won't be called in this case). My solution is based on the time spend between onCreate
and onResume
.
If the time between onCreate
and onResume
is shorter then a threshold (I use 1s assuming onCreate
could be finished within 1s which we really wanted for the good programming practice), then we believe this is the first time the onStart
/onResume
is called right after the onCreate
, otherwise we believe it is about all the other cases including our motivating case.
Then we only need to set the current timestamp in onCreate
and do the time comparison in onResume
to make the decision to set the current item of ViewPager
to default or current. Note that we need to save the current page number in onPause
in SharedPreference
.
I am not claiming this idea is perfect but it works fine for me so far. Let me know if you are interested in the implementation and I will post my code upon your request.
Upvotes: 1
Reputation: 6905
After you have initialised your viewpager, use this method :
viewPager.setCurrentItem(int position)
Or this :
viewPager.setCurrentItem(int position, boolean withAnimation)
You can save the last position by using SharedPreference
in the onPageSelect()
method where you can get the position of each page. You have to implement the OnPageChangeListner
in order to have this method.
Edit
Your question wasn't clear :
Sorry, maybe I didn't express my problem well enough. I want the starting page to appear everytime I start my app. But if I enter the activity by the back-button (for example if I opened my settings activity and then go back) the last viewed page should be shown. The solution provided in the link will lead to the safed page everytime I open the app
I don't know why you want to change this, it's a normal behavior, read this.
But if you insist, you can always use setCurrentItem()
method in the onResume
of your Activity/Fragment
, thus the first page will always be shown after your Activity/Fragment
gets resumed.
Edit 2
That can still be done by setCurrentItem
. In your adapter, try to detect the index
of the page
of the current day
. Create a method that returns that field from outside the adapter. And then after you have initialised your ViewPager
,
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(adapter);
setCurrentItem(adapter.getCurrentDayPosition()) // or something like that ...
The method in the adapter :
public int getCurrentDayPosition() {
return this.currentDayPosition // this a field of the adapter.
}
Upvotes: 3
Reputation: 1415
i think this solve your problem , because first time you launch your activity there is no instance saved (savedInstanceState) ... read comments in the post ...Android :Save View Pager State ...
Upvotes: 0