Bushes
Bushes

Reputation: 1010

Webview saving state of page

I have a small problem with a webview I'm using. I'm trying to use the webview to allow the user to fill out a registration form. This works fine when the user completes the form in one session but should the user lock their phone, the webview activity is destroyed.

I have two activities in memory, one of which is quite memory intensive. The problem I believe I'm having is that because this other activity requires more memory than the webview, the webview is destroyed.

I'm fine with this to a degree, but I would really like to be able to save the current state of the web page so the user doesn't have to submit all of their details again. Is there anyway I can do this.

Thanks

Upvotes: 2

Views: 7312

Answers (4)

user3071918
user3071918

Reputation: 21

We have run into a similar issue. We have an activity with a webview.
We want the user to input 3 fields on a form in the web view, then launch a new activity (camera) to take a picture. When we get the picture back, and using javascript we insert the picture into the currently active webview form. It all works great 99.9% of the time. However, in some odd scenarios which are difficult to reproduce, while the camera activity is active, the webview activity behind it gets destoryed. saveState / restoreState only partially restores the webview. The 3 form fields that the user had inputted but not submitted when we launched the camera are now lost. They do not get saved by webview.saveState. Also, some javascript state on the page is lost too.

To work around this we added a javascript method to the page and invoke it from the app on saveInstanceState. Then after we restore the state, we invoke another javascript method on the webview page to put the state back. It is a pain in the but and requires work for each form you need to do that to, but it works.

I hope future versions of Android webview.saveState save it exactly as it was before the activity gets destroyed. Alternatively it would be great if you mark an activity as critical to your app and only allow that activity to be destroyed if your app is not the current app the user is working with.

Upvotes: 2

afpro
afpro

Reputation: 1103

try WebView.addJavascriptInterface, when Activity.onPause, call javascript method in page by loadUrl("javascript:..."), and in web page, call native method that provided by WebView.addJavascriptInterface to save data. is this what you want?

Upvotes: 1

Yogesh Tatwal
Yogesh Tatwal

Reputation: 2751

try it :- You can use WebView method saveState()/restoreState() and save it to activity onRestore bundle and use saved value in onCreate method if the previous Activity will be destroyed. You can use similar method which is described here (but used for only configuration change): http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/

Upvotes: 3

Narkha
Narkha

Reputation: 1197

An activity can be destroyed when back key is pressed; when the screen is rotated (is faster destroy the activity and recreate that resort all the graphic elements); or when is posible that it will not active in a few time (lock the phone, press home key or other application is started).

You need to do that @Ixx suggest in his comment : Android webView saveState

Save the data of your application with something like

@Override
public void onSaveInstanceState(Bundle outState) {          
    super.onSaveInstanceState(outState);   
    /// save your things, e.g.
    outState.putString("name", name);
}

And recover, it exists, in the method onCreate

@Override
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    // if it is a new activity
    if (savedInstanceState == null) {
        // initialize vars, e.g.
        name = "Unknown";
    }
    // if is a call after detroy the activity
    else {
        // recover vars, e.g.
        name = savedInstanceState.getString("name");
    }
}

Upvotes: -1

Related Questions