CarlLee
CarlLee

Reputation: 4182

What's the best practice for persist session between Activities in Android?

I have a HttpClient to communicate with my server, and I want to persist my session to keep user logged between Activities.

Up till now, I have used a Service to back up my HttpClient, including a background thread to do the heavy work.

Another way to do this I know through my googling is that save the Cookie in a local file/xml/database, and check if it exists, if it exists, then use it in the new HttpClient I created each Activity.

Which way is better or are there even better ways to achieve what I want?

Upvotes: 2

Views: 1081

Answers (1)

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

I personally would go with the cookie. Cookies are commonly used to do just this. HttpClient accepts a CookieStore that you can have backed by a file. When the cookie is updated, just update the file and when it has to be retrieved either use an in memory copy or read the file. I don't know how your would back up your session with just a service. The Service would have to save some state somewhere. Serializing an HttpClient does not seem to be possible. So I would just implement a CookieStore.

CookieStore

Upvotes: 2

Related Questions