Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the state of a WebView and reloading the position

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!

like image 912
user1363871 Avatar asked Apr 18 '15 14:04

user1363871


1 Answers

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);
like image 63
ToYonos Avatar answered Oct 23 '22 01:10

ToYonos