Dovydas Rupšys Dovydas Rupšys - 1 year ago 106
Java Question

Android Force Update Webview Session

I have an android application with a Webview embedded and a website that this application connects to. When logging in (or logging out) to the user the session data seems to take about a minute or two before it is stored in permanent memory.

The result of this is that if after login (or logout) the app is immediately turned off then the session data does not become persistent and opening the app again later reverts to the previous session.

This looks like some kind of a caching issue, I've looked around for a solution but I can't seem to be able to find any way to force update session data, I would be grateful if someone could point me in the right direction.

Answer Source

I've ended up using the following code in my JavascriptInterface

        //Not sure if this part works haven't tested it yet
        CookieSyncManager sm = CookieSyncManager.createInstance(context);
    } else {

CookieSyncManager is obsolete in lollipop, call flush on CookieManager instance instead. This will save cookies in memory to permanent storage. Note that this is still done asynchronously therefore you can't run it onDestroy() it still takes a couple of seconds.

I've put this in the @JavascriptInterface function which is called by the javascript whenever user logs in or logs out and it seems to work well enough.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download