0

I'm using ViewPager.setCurrentItem(int i) to change page of the a ViewPager with two pages, I'll call them page 0 and 1, but it seems not work after the activity onPause() caused by android device's screen off.

Here is my scenario:

  1. change the viewpager to page 1 in onPause()
  2. change back to page 0 after 5 seconds (activity in background)
  3. when come back, I want the viewpager showing page 0

Strange result:

  1. when I jumped to another activity and came back after 5 seconds, it worked well, i got page 0
  2. when I turned off the screen and turned on after 5 seconds, i got page 1, even the pageChangeListener told me that the pager had already selected 0

I want to know the real reason or some details about the difference between these two results. Any help will be greatly appreciated.

It looks like this question, but it's not exactly what i want to kown and not the duplicate question.

Community
  • 1
  • 1
Jerome
  • 1,749
  • 1
  • 14
  • 40

1 Answers1

0

I think this may help you in your onPause:

yourViewPager.setOffscreenPageLimit(MAX_POSSIBLE_PAGES);

Basically, it forces the pager to retain all possible pages when idling. That way, when you resume, your indices for setCurrentItem() make sense.

JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19