How do I save an Android application's state?

Posted by Bernard on Stack Overflow See other posts from Stack Overflow or by Bernard
Published on 2008-09-30T04:41:15Z Indexed on 2010/05/07 0:28 UTC
Read the original article Hit count: 600

Filed under:

I've been playing around with the Android SDK, and I am a little unclear on saving an applications state. So given this minor re-tooling of the 'Hello, Android' example:

package com.android.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextView = new TextView(this);

        if (savedInstanceState == null) {
            mTextView.setText("Welcome to HelloAndroid!");
        } else {
            mTextView.setText("Welcome back.");
        }

        setContentView(mTextView);
    }

    private TextView mTextView = null;
}

I thought that might be all one needed to do for the simplest case, but it always gives me the first message, no matter how I navigate away from the app. I'm sure it's probably something simple like overriding onPause or something like that, but I've been poking away in the docs for 30 minutes or so and haven't found anything obvious, so would appreciate any help.

Cue me looking a fool in three, two, one...

Thanks.

© Stack Overflow or respective owner

Related posts about android