I have a need to integrate an app which has a native login screen with the rest of the application running inside of a webview (hybrid app). It sounds like a somewhat common approach but I'm having problems transferring session data (cookies) from native code to the webview and I think it's related to the asynchronous behaviour of CookieManager.
Sometimes, on some devices, the cookies are either being removed or not applied. From what I've read it could be because removeSessionCookie, setCookie and sync run asynchronously in their own thread. I don't really understand this in Java coming from other programming languages as there doesn't seem to be any hooks to know when then task is complete EG callbacks, events, asyc/await etc.
So the question is how do you know when a async task is done in Android/Java? I've come across the synchronized block syntax but it doesn't look like it would wait for something like removeSessionCookie to complete.
My code looks a little like this:
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie(); // problem
CookieSyncManager.getInstance().sync(); // maybe problem
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
Cookie cookie = cookies.get(i);
String cookieString = cookie.getName() + "=" + cookie.getValue();
cookieManager.setCookie(url, cookieString);
}
CookieSyncManager.getInstance().sync();