Saturday, 18 June 2016

android - How to save an activity state using save instance state?

I've been working on the Android SDK platform, and it is a little unclear how to save an application's 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 {

private TextView mTextView = null;

/** 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);

}
}


I thought it would be enough for the simplest case, but it always responds with the first message, no matter how I navigate away from the app.



I'm sure the solution is as simple as overriding onPause or something like that, but I've been poking away in the documentation for 30 minutes or so and haven't found anything obvious.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...