10

I know there are multiple questions about this but they all seem to be old and not working anymore - at least for me. I am trying to save the position of a webview after a user quits or reloads the app. I know that this code could be used to save and load the Webview:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    myWebview.saveState(outState);
}

and in onCreate:

if (savedInstanceState != null) {
    myWebview.restoreState(savedInstanceState);
}

But, in the Android Developer Webview page it says the following about saveState:

"Saves the state of this WebView used in onSaveInstanceState(Bundle). Please note that this method no longer stores the display data for this WebView. The previous behavior could potentially leak files if restoreState(Bundle) was never called."

Link - https://developer.android.com/reference/android/webkit/WebView.html

If I am wrong and this is SUPPOSED to be working, it may be my implementation of another function that saves the spinner selection of the page shown in the WebView and is loaded in a very similar way that is shown above. Perhaps using both doesn't allow the position of the WebView to be loaded and only the page? Here is what I have currently to save which HTML page is loaded into the WebView when the user reloads the app.

My onCreate:

if (savedInstanceState != null){
    mySpinner.setSelection(prefs.getInt("spinnerSelection", 0));
}

my onSaveInstanceState:

@Override
public void onSaveInstanceState(Bundle savedInstanceState){

    SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
    SharedPreferences.Editor editor = getPrefs.edit();

    values = Integer.parseInt(getPrefs.getString("pref_Sections", "1"));
    switch (values) {
        case 1:
            int selectedPosition1 = mySpinner.getSelectedItemPosition();
            editor.putInt("spinnerSelection1", selectedPosition1);
            editor.commit();
            break;
        case 2:
            int selectedPosition2 = mySpinner.getSelectedItemPosition();
            editor.putInt("spinnerSelection2", selectedPosition2);
            editor.commit();
            break;
    }
    super.onSaveInstanceState(savedInstanceState);
}

Does anyone have any feedback on this issue, or is it just not working anymore as described in Android Developer WebView? I was considering a math workaround but at the moment that is about out of my expertise and I'd have to learn more about that (plus I dislike math in general!)

Thank to all replies!

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
user1363871
  • 580
  • 3
  • 11
  • 29
  • What is position of Webview ? Is it the same as doing url#position on browser ? – inmyth Apr 18 '15 at 15:04
  • How far down in the webpage the user has read the content. I want that position to be saved and reloaded when the user comes back to the application. – user1363871 Apr 18 '15 at 16:15
  • are you able to modify the HTML/Javascript side of this, or can you modify only on the client/Webview side? – EpicPandaForce Apr 21 '15 at 15:14
  • I'm not experienced with JavaScript but I could. The HTML files are stored locally in Assets. – user1363871 Apr 21 '15 at 18:55
  • It is not probabadly the best way, but u can override the onTouchEvent and track the movement and the use the scroll method of webview – D4rWiNS Apr 22 '15 at 14:23
  • Thanks @D4rWiNS but I was hoping a simpler solution would be out there. For what I'm trying to achieve that seems to be a bit of a workaround to get there. – user1363871 Apr 22 '15 at 22:36

1 Answers1

6

You can use getScrollY() to get the vertical position of your webview. Save it in onSaveInstanceState

Then, use scrollTo(x, y) to restore the position :

 yourWebview.scrollTo(0, savedYPosition);
ToYonos
  • 16,469
  • 2
  • 54
  • 70