Java – saves the state of the WebView and reloads the location
I know a lot about this, but they seem old and no longer work - at least for me I try to save the location of the web page view after the user exits or reloads the application I know this code can be used to save and load webviews:
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); myWebview.saveState(outState); }
And in oncreate:
if (savedInstanceState != null) { myWebview.restoreState(savedInstanceState); }
However, on the Android Developer WebView page, it explains the following about savestate:
"Save the state of this WebView used in onsaveinstancestate (bundle). Please note that this method no longer stores the display data of this WebView. If restorestate (bundle) is not called, the previous behavior may potentially leak the file
Link – https://developer.android.com/reference/android/webkit/WebView.html
If I'm wrong, it's working. Maybe I implement another function to save the spinner selection of the page displayed in WebView and load it in a very similar way to that shown above Perhaps using both does not allow WebView locations to be loaded, only pages? When the user reloads the application, I am currently saving which HTML pages to load into WebView
My creation:
if (savedInstanceState != null){ mySpinner.setSelection(prefs.getInt("spinnerSelection",0)); }
My onsaveinstance status:
@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 does it just stop working as described in Android Developer WebView? I am considering a mathematical solution, but at this moment, my professional knowledge is limited and I must know more (plus I don't like mathematics in general!)
Thank you for all your replies!
Solution
You can use getscrolly () to get the vertical position of your WebView Save it in onsaveinstancestate
Then, use scrollto (x, y) to restore the location:
yourWebview.scrollTo(0,savedYPosition);