Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ViewPager save previous page on onPageSelected

I have a ViewPager with pages of WebView. A page has a text field allowing the user to type in contents. I'd like to save the page's input text to a Java variable when moving between pages (by sliding forward/backward).

Unfortunately, it seems onPageSelected only tells me the page number that is about to display, and not the page it came from, which means I can't tell which page I needed to save at the time when the user changes page.

Any ideas? Many thanks in advance!

like image 210
iht Avatar asked Jan 01 '14 00:01

iht


1 Answers

You can store the last known page index in a field at the end of onPageSelected (initialize it with an appropriate default when your activity starts). Then, inside onPageSelected, you check that field to see what page the user was previous on.

private int mLastPage;

@Override
public void onPageSelected(int position) {
    // save the information from the page at mLastPage

    mLastPage = position;
}
like image 174
Tony Allevato Avatar answered Oct 05 '22 23:10

Tony Allevato