3

I am working on an app which talks to server and have a login screen. I am using httpconnection to communicate with server (server sends Json response back). My login screen send email and password, which is validated by server - which sends the cookie and expects the cookie with each further request to know that user is logged in.

I am using Cookiemanager and cookie handler to enable the session so that user have to login once only.

Using below 2 commands in my Main Activity (on Create method)

CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);

Working model is that my main activity sends a read command to server, which checks whether user is already logged in or not (expecting cookie with id inside), and send status in json. If status is 0, it moves to login activity otherwise to home screen.

In login activity, I am sending the login command with username and password and server sends a cookie(sends a unique id, which it cross checks on any further request to validate the user) back with json response.

After login, I move to home screen and read the status and since I created the CookieManager in the main activity, it sends the cookie with each httprequest automatically.

Problem is -- When I exit out of the application and come back after doing the login.. it again starts with main activity and goes to login screen instead of home screen. I am not able to figure out why is that and how to make my user log in once and after that it retains the cookie even if he exits out of the app.

I thought CookieManager persists the cookie even after you exit. Is it because I am creating the cookie Manager object in on create method and when app is exited and relaunched it again creates the new object? Or my assumption is wrong.

miensol
  • 32,791
  • 6
  • 103
  • 105

2 Answers2

1

By default new CookieManager() will store cookies in memory.

However you can use different constructor to change this behavior: CookieManager(CookieStore store, CookiePolicy cookiePolicy).

There are at least couple of persistent java.net.CookieStore implementations:

Community
  • 1
  • 1
miensol
  • 32,791
  • 6
  • 103
  • 105
  • Thanks for the reply! So, by default using CookieManager, won't be able to retrive the cookies when app is closed or killed, if it stores in the memory? – Sumit Mittal Jan 01 '16 at 22:50
  • Yes, when the app process quits the cookies are gone when using in memory cookie store – miensol Jan 02 '16 at 07:51
-1
If I move the Cookiemanager to the application class, it's just get called once and solve this issue. 

public class application extends Application {
    private static Context context;
    @Override
    public void onCreate() {
        super.onCreate();
            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);
    }

    public static Context getContext() {
        return application.context;
    }
  • what are talking about? they are still saved only in memory, after restarting the app all cookies are gone – user924 Oct 12 '18 at 08:06