1

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Android
  • 1,085
  • 4
  • 13
  • 28

2 Answers2

0

Try this snippet of Code -

public class Tab2Fragment extends Fragment {

    private WebView webView;
    private Bundle webViewBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.tab2,
        container, false);

    webView = (WebView) ll.findViewById(R.id.webView1);
    webView.setWebViewClient(new WebViewClient());

    if (webViewBundle == null) {
        webView.loadUrl("http://www.lucazanini.eu");
    } else {
        webView.restoreState(webViewBundle);
    }

    return ll;

    }

    @Override
    public void onPause() {
    super.onPause();

    webViewBundle = new Bundle();
    webView.saveState(webViewBundle);
    }
}

Or Try using this way -

If website is starting session,then you can use this to get session and save it to Shared Preferences. Otherwise you can keep track of page changes, and use SharedPreferences to save/retrieve latest page.

Al-Amin
  • 1,369
  • 1
  • 9
  • 26
  • I already tried the above example and it doesnt work :( and for other options you mentioned to save cookies and keep track of page changes i am not getting any callback for the page change as there is no change in the url or it doesnt open a new url its simply a chat screen where user is chatting with customer care. – Android Nov 07 '18 at 12:22
  • https://www.codexpedia.com/android/javascript-interface-for-android-and-javascript-communication/ check this url hope it will help you. – Al-Amin Nov 07 '18 at 12:32
  • It is calling some function using java-script, What i need is a way to save this fragment/Web view state and whenever this fragment show this fragment/web view from the saved state not from the beginning Just like a singleton class – Android Nov 07 '18 at 12:38
  • You may keep some params in your url to save state. – Al-Amin Nov 07 '18 at 12:42
0

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.

Android
  • 1,085
  • 4
  • 13
  • 28