OhStack
OhStack

Reputation: 116

Clear session of WebView once the app closes or terminated

Integrated SSO Login(single sign on) in WebView, Once the user logged in I am getting the parameters from redirected url (if user exists) and moving to the home page of the app. Once the app closed, terminated or logged out I want clear the session of WebView, As of now I am getting directly redirected url because of session is not expired.

These solution doesn't worked for me

webView.clearCache(true);

webView.clearHistory();

webView.destroy();

Upvotes: 1

Views: 2693

Answers (2)

user22349326
user22349326

Reputation:

In order to clear the webview cookie, Use the below code.

    // Clear all the Application Cache, Web SQL Database and the HTML5 Web Storage
    WebStorage.getInstance().deleteAllData();

    // Clear all the cookies
    CookieManager.getInstance().removeAllCookies(null);
    CookieManager.getInstance().flush();

    webView.clearCache(true);
    webView.clearFormData();
    webView.clearHistory();
    webView.clearSslPreferences();

Upvotes: 1

Mehul Solanki
Mehul Solanki

Reputation: 1141

I think clear cookie make can make session close. Try this.

CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();

Another way

static void clearWebViewAllCache(Context context, WebView webView) {
  try {
    AgentWebConfig.removeAllCookies(null);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    context.deleteDatabase("webviewCache.db");
    context.deleteDatabase("webview.db");
    webView.clearCache(true);
    webView.clearHistory();
    webView.clearFormData();
    clearCacheFolder(new File(AgentWebConfig.getCachePath(context)), 0);
  } catch (Exception ignore) {
    //ignore.printStackTrace();
    if (AgentWebConfig.DEBUG) {
      ignore.printStackTrace();
    }
  }

}

Upvotes: 2

Related Questions