7

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.

Antonio
  • 221
  • 1
  • 4
  • Hi, i was thinking of "user closes out of my app", does this meant that app is being totally close from app manager (square or overview button)? if yes, i think it is not possible, since webview is being destroy the time app was closed. – Ric17101 Dec 15 '20 at 09:32
  • Hello, "Close out of my app" means app go to background, . So when you go back to your application the onResume method will be called instead of onCreate. So put your code on OnResume. – puspak saha Dec 16 '20 at 10:43
  • https://stackoverflow.com/questions/18479519/how-to-save-restore-webview-state ... how would you do this with Chrome or Puppeteer? Also, that concept doesn't consider that forms or access tokens may long have timed out on the server side. – Martin Zeitler Dec 17 '20 at 09:53

2 Answers2

0

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

You could using this code in your custom WebView:

override fun onWindowVisibilityChanged(visibility: Int) {
        if (visibility != View.GONE) super.onWindowVisibilityChanged(View.VISIBLE)
} 
forest
  • 31
  • 4