0

I'm new to the Android ViewPager.

I have a ViewPager with WebViews on each page - basically slides of web pages. Some of the pages have text input. I want to save the user input of the pages into a Java object, so I can send them to the server, as well as restore them later when reopening the ViewPager.

Any idea how to go about doing this? Originally I thought I should save whenver I change pages (OnPageChangeListener), but I can't figure out how to pull up the Fragment of the last page, so I can get hold of the input fields from the WebView to save. I need the save to occur so that the ViewPager always displays the correct data, e.g. when sliding between pages, switching orientations, pause/resume app, etc.

Thanks very much in advance.

iht
  • 692
  • 1
  • 8
  • 15

2 Answers2

0

well can go like this

  1. Catch the user input using javascript bridge,have a database to store all the user inputs.
  2. Then use the same user inputs while sending to server, or while restoring the webpages.
Sush
  • 3,864
  • 2
  • 17
  • 35
  • I understand both points. My question is how to actually get access to a Fragment's WebView when changing pages, so I can save the user input in the first place. The onPageSelected doesn't give me access to the previous Fragment, which I need to get the WebView. Does this make sense? – iht Jan 02 '14 at 07:07
  • ok.. you can solve it, have some id for webview and pass it when ever ur storing values in the data base. – Sush Jan 02 '14 at 07:11
  • Would that work? The way I've got it is there's an Activity hosting a ViewPager, which in turn contains Fragments, each one with a WebView. The Adapter's getItem() returns the Fragment, with a new WebView. Now, I thought the Activity is recreated whenever the orientation changes, and therefore everything inside it is recreated? Sorry, but I'm confused as to how this works in practice. Thanks again. – iht Jan 02 '14 at 07:19
  • then ur problem is activity recreation, than u can handle by setting onconfigchange attribute in the android manifest file for the activity tag. like this android:configChanges="orientation|keyboardHidden" – Sush Jan 02 '14 at 07:28
  • Or is there a way to iterate through all the fragments in the ViewPager? – iht Jan 02 '14 at 07:32
  • do something which will work today and tomorrow. use data base it will not break. – Sush Jan 02 '14 at 07:34
  • Pulling out the webview by the ID and changing the orientation also sound like a hack here though - see comments here (http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation) – iht Jan 02 '14 at 11:30
  • oh ok.. then dont set that. but webview can have ids na.. like while storing in to data base u can save it like id as login and name and value pairs. – Sush Jan 02 '14 at 11:35
0

OK, I think I have figured it out:

Rather than saving when flicking between pages, I'm now saving the data in the onPause() of the Fragment itself, i.e. the Fragment for each page. The Fragment's onPause() is called when:

  • The Fragment is about to be destroyed (when exiting the ViewPager, or when the Fragment is outside of the ViewPager's cached range)
  • When the app itself is paused, e.g. switching to another app, or when it's killed
  • When a child Activity is opened
  • When the orientation of the device changes, causing the parent Activity to be recreated

This seems to the most reliable way of saving a Fragment's data.

Any comments or suggestions on this?

Thanks again.

iht
  • 692
  • 1
  • 8
  • 15