tags:

views:

31

answers:

1

Hi all, I posted a question yesterday about determining when an app is being finalized vs destroyed for screen orientation change. Thanks to the answers I received I was able to resolve my problem with the screen orientation change. However, I am still running into a roadblock. This app I am working on logs into a website with an HttpClient. As long as the app remains in memory the HttpClient will retain the cookies from logging in. However, once it is killed, it would need to log in again. My question: How can I determine when the app is being killed from memory so I can set a boolean to false telling the app it has been removed from memory so the next time it starts it will read this and determine is must log in again? Or is it possible to serialize an HttpClient and put that in the savedInstanceState bundle? May extract the cookies from the client and put those in the savedInstanceState bundle? Is there something I'm completely missing here maybe? Any help or a point in the right direction is greatly appreciated because this one has me stumped. Thank you!

+1  A: 

How can I determine when the app is being killed from memory so I can set a boolean to false telling the app it has been removed from memory so the next time it starts it will read this and determine is must log in again?

IMHO, that's the wrong question.

The right question is: "how can I tell that the cookie is gone?"

This is much closer to the real business problem ("how do I know if I have to log in again?") and therefore will cover more potential edge and corner cases than assuming that some system event is the only source of the cookie vanishing.

Besides, it's easier: your HttpClient object can give you access to the CookieStore, and you can iterate over those cookies to see if yours is in there. Admittedly, it'd be nice if they had a more direct method on that class for such a lookup.

So, before you do an access to the Web app, check to see if the cookie is there, and pop up the login dialog/activity/whatever if needed.

Or is it possible to serialize an HttpClient and put that in the savedInstanceState bundle?

No.

May extract the cookies from the client and put those in the savedInstanceState bundle?

Yes, but then you'll have a devil of a time putting them back in the CookieStore, IIRC.

CommonsWare
CommonsWare,Thank you, your advice was most helpful. I think I have it all sorted now.To anyone that runs across this question looking for some code, this is what I came up with: (excuse the lack of line breaks) if(((AbstractHttpClient)httpClient).getCookieStore().getCookies().isEmpty()) { // There are no cookies; must log in again } else { // There are cookies; no need to log in again }
Matt