Android WebView Save Display State when app closes

Viewed 518

I want to be able to save the state of a WebView when a user closes out of my app and be able to restore it to exactly how it was when the user returns to the app. I want it to restore websites with forms, online calculators that store the history of calculations, or any websites that make Javascript objects dynamically. How can I do this? Below are some of the methods I have tried.

I have tried using the saveState method found here but the behavior has been changed.

Please note that this method no longer stores the display data for this WebView.

I want to be able to restore the display data.

I have tried the saveWebArchive method here but when saving and restore a web archive version it doesn’t put the display data back exactly how it was.

I am not able to use any of the HTML5 storage methods or evaluateJavascript to handle this since I want to be able to save and restore the state for any website I visit.

2 Answers

When sending the application to the background, it is enough to load the URL once only:

@Override
public void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState == null) {
        webView.loadUrl(defaultUrl);
    }
}

But when terminating the application, the restore capabilities are rather limited.

You could using this code in your custom WebView:

override fun onWindowVisibilityChanged(visibility: Int) {
        if (visibility != View.GONE) super.onWindowVisibilityChanged(View.VISIBLE)
} 
Related