Android ViewPager save previous page on onPageSelected
Asked Answered
M

1

8

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!

Mccarthy answered 1/1, 2014 at 0:11 Comment(0)
O
16

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;
}
Oui answered 1/1, 2014 at 0:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.