In Android, we can have different process (e.g. a Service in another process).
However, I notice the CookieManager (from android.webkit.CookieManager) doesn't sync its newly set Cookie across the process.
I have a function in the MainActivity that set the Cookie
cookieManager.setCookie(url, cookieString)
cookieManager.flush()
If I read it in the MainActivity or MainService (service in the same process), then I can get it read.
val cookie = cookieManager.getCookie(url)
However, if I read it in the OtherService (service in the other process), then it might not get it.
To demonstrate this clearly, I have created an App as shown below. You can get the code here
1. Freshly Install App
- Install a freshly created App
- Click Main Process Service: Log (TrackingMe) shows no Cookie Set <Correct>
- Click Other Process Service: Log (TrackingMe) shows no Cookie Set <Correct>
- Click Write Cookie to write cookie into CookieManager
- Click Main Process Service: Log (TrackingMe) shows Cookie Set <Correct>
- Click Other Process Service: Log (TrackingMe) shows no Cookie Set <Incorrect>
2. Kill the App and Restart the App
- Kill and Restart the App (don't remove the App data)
- Click Main Process Service: Log (TrackingMe) shows Cookie Set <Correct>
- Click Other Process Service: Log (TrackingMe) shows Cookie Set <Correct>
- Click Clear Cookie to clear all Cookies into CookieManager
- Click Main Process Service: Log (TrackingMe) shows no Cookie Set <Correct>
- Click Other Process Service: Log (TrackingMe) shows Cookie Set <Incorrect>
The finding
- The Other Process Service will not get the updated cookies from the CookieManager. It can only get it after the App is killed and restart.
The question:
- How can we get the Other Process Service get the Cookies from the CookieManager immediately? (without need to restart the App?)