androidandroid-fragmentsandroid-activitywebviewsavestate

Singleton Fragment or save web view state


Here what i want is i have a fragment inside an Activity and it has a web view which is basically chat to customer service. This activity will be started from a button in another activity,

Now the tricky part is i want to save the current session of the web view so that whenever user comes to this activity or whenever i use this fragment i can show the last conversation as well.

So what i have tried is

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // Saving the last loaded url in shared pref and then loading the same url in oncreateview
        return true;
    }
}

But when user is having a conversation it is not loading different url thus shouldOverrideUrlLoading is never called.

Help will be highly appriciated


Solution

  • As Al-Amin said i could have saved some keys in my URL but i had no support for this project from back end team as this module is used at many other places so they couldn't change anything so I solved this by using launch mode for my activity as single Instance

    android:launchMode="singleInstance"
    

    and for the on back press

    moveTaskToBack(true);
    

    Thus my activity with the webview is not destroyed until i call finish the activity.