0

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.

Spooky
  • 2,966
  • 8
  • 27
  • 41
Leif Sabellek
  • 173
  • 1
  • 9
  • Question already answered : see : http://stackoverflow.com/questions/19048693/android-save-view-pager-state – issamux Mar 17 '14 at 22:42
  • I think this does not answer my question, because I do not want to save the page number everytime. I want to save it only if the user goes to another activity inside my programm and then comes back. If he restarts the app from the launcher, there should be always the starting page. – Leif Sabellek Mar 17 '14 at 22:53

3 Answers3

3

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.
}
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
  • I updated my question, so now it should be clear. I know that I can use the setCurrentItem in onResume() but if I do this, the page will be shown everytime. The reason I want to change it: Every page contains information about a day and when you start the app, there should be always the current day. You can swipe left and right to see other days, you can open settings and go back, there should be the same day. But if you start the app, there should always be the page of today. – Leif Sabellek Mar 17 '14 at 23:39
  • Ok I see. But that can be done by `setCurrentItem`. When initialising your pager, 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 initialising your viewpager, setCurrentItem(adapter.getCurrentDayPosition()) or something like that ... – S.Thiongane Mar 17 '14 at 23:55
  • But the adapter and the pager also get initialized when I come back from my settings activity, no? So this would lead me to today's page everytime. – Leif Sabellek Mar 18 '14 at 00:00
  • No ! this is just for when starting app the first time. So you must at the above codes in `onCreate`. When you come back from settings, you will normally be on the same state of the pager. You have nothing to do here ! – S.Thiongane Mar 18 '14 at 00:05
  • Im confused now. So the onCreate method should not be called, when I come back from the settings? I just checked it and my onCreate method gets called everytime I come back from the settings. And also when the screen is rotated onCreate gets called. This is a normal behaviour, right? – Leif Sabellek Mar 18 '14 at 00:07
  • No, you are mixing things up :). When the screen rotates, the configuration changes, so the activity will be recreated from its beginning, that's why onCreate will be called again. But when you open another activity B, the previous activity A will be onPause, and when you tap the back button, the A activity will call onResume but not onCreate, until if you defined FLAGS such as FLAG_ACTIVITY_CLEAR_TOP when launching A activity, or when declaring it in manifest. In this case, the activity can be recreated. – S.Thiongane Mar 18 '14 at 00:23
  • I just checked it again: onCreate is definitely called when I return from other activities inside my app. And I don't have any FLAGS defined. And apart from this, of course the page should not change when I rotate the device, so just putting it in onCreate is still not solves the problem. – Leif Sabellek Mar 18 '14 at 00:33
  • [this post](http://stackoverflow.com/questions/11347161/oncreate-always-called-if-navigating-back-with-intent) may help you understand. Good luck – S.Thiongane Mar 18 '14 at 00:40
1

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.

Spooky
  • 2,966
  • 8
  • 27
  • 41
chyz198
  • 81
  • 1
  • 2
0

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 ...

Community
  • 1
  • 1
issamux
  • 1,336
  • 1
  • 19
  • 35
  • 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. – Leif Sabellek Mar 17 '14 at 23:27