Persisting Cookies through Activity Life Cycle

If you are coding an Android REST application using the Apache HttpClient, there may be times when you bring another application into the foreground (like Google Maps to get driving directions), and the process containing your application is killed because of low system memory. Your instance of the HttpClient is dead, and so it no longer has the session information cookies needed to run the app.

After the process is killed, navigating back to your activity causes onCreate() to be called (Application Fundamentals). Thus, in the onCreate() method, we can first check to see if the session cookies are gone, and if so, re-add them to the HttpClient.

In order to put the Cookie in a Bundle, we must make it serialiazable. Unfortunately Cookie is not serializable in the Apache API version that Android currently uses, so we must define our own class to do this.

public class SerializedCookie implements Serializable {

	private static final long serialVersionUID = 5327445113190674523L; //arbitrary

	private String name;
	private String value;
	private String domain;
	
	public SerializedCookie(Cookie cookie){
		this.name = cookie.getName();
		this.value = cookie.getValue();
		this.domain = cookie.getDomain();
	}
	
	public String getName(){
		return name;
	}
	
	public String getValue(){
		return value;
	}
	public String getDomain(){
		return domain;
	}
}

Now in our Activities we need to override the onCreate() and onSaveInstanceState() methods.

DefaultHttpClient client; //declared here, but get the client however you must. 

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		List<Cookie> cookies =client.getCookies();
		if (!cookies.isEmpty()){
			Cookie sessionInfo = cookies.get(0);
			outState.putSerializable("sessionInfo", new SerializedCookie(sessionInfo));
		}
		
 DefaultHttpClient client; //declared here, but get the client however you must. 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if (client.getCookies().isEmpty()){
			if (savedInstanceState.containsKey("sessionInfo")){
				SerializedCookie cookie = (SerializedCookie) savedInstanceState.
					getSerializable("sessionInfo");
				
				BasicClientCookie newCookie = new BasicClientCookie(cookie.getName(),
						cookie.getValue());
						newCookie.setDomain(cookie.getDomain());
						
				client.addCookie(newCookie);
			} else {
                                //for whatever reason the session information couldn't be obtained,
                                //take action here
			}
		}

Now if you open another application and the process running your app is killed, when you navigate back to it, your activity will try to re-insert the session cookie into the HttpClient, and your app will run normally.

About these ads
This entry was posted in Android and tagged , , , , , . Bookmark the permalink.

2 Responses to Persisting Cookies through Activity Life Cycle

  1. Hey!

    I just dropped by to say thanks for the great tutorial, now my app doesn’t crash anymore. :-)

    Best regards,
    Karl Lindmar

  2. Alex says:

    Good job, thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s