Android Camera Intent destroys CookieManager session

90 views Asked by At

For some reason, CookieManager sometimes loses the session variable when I open a camera intent and return from it. This doesn't happen all the time but it does happen often. I can confirm that the session variable is being lost because my backend web server is complaining about it.

This is my code that I put on my MainApplication extends Application class in the onCreate() method.

CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);

I would appreciate if anyone can give me some insight into what is happening.

Update:

I tried storing the cookies using the following code but it did not work!

public static void GetCookies(Context context){
    SharedPreferences prefs = QuickFunctions.getPreferences(context);
    CookieManager ckManager = (CookieManager) CookieHandler.getDefault();
    CookieStore ckStore = ckManager.getCookieStore();

    List<URI> uriList=ckStore.getURIs();



    List<HttpCookie> cks = ckStore.getCookies();



    boolean sessionExists=false;

    for (URI uri : uriList) {
      if (uri.getHost().contains("testsite.ca")){
            sessionExists=true;
        }
      }
    if (sessionExists==false){
        if (prefs.contains("sessionURI") && prefs.contains("cookieName") && prefs.contains("cookieValue")){
            URI intuchURI = URI.create(prefs.getString("sessionURI", "defaultString"));
            HttpCookie ck = new HttpCookie( prefs.getString("cookieName", "defaultString"), prefs.getString("cookieValue", "defaultString"));
            ckStore.add(intuchURI,ck);

        }

    }
}

public static void SaveCookies(Context context){
    SharedPreferences prefs = QuickFunctions.getPreferences(context);
    Editor edit=prefs.edit();
    CookieManager ckManager = (CookieManager) CookieHandler.getDefault();
    CookieStore ckStore = ckManager.getCookieStore();

    List<URI> uriList=ckStore.getURIs();

  for (URI uri : uriList) {
    if (uri.getHost().contains("testsite.ca")){
        edit.putString("sessionURI", uri.toString());
        List<HttpCookie> cks = ckStore.get(uri);
        for (HttpCookie ck : cks) {
            if (ck.toString().contains("sessionid")){
                edit.putString("cookieName",ck.getName());
                edit.putString("cookieValue",ck.getValue());
            }
          }
    }
  }

  edit.commit();



}

Thanks!

0

There are 0 answers