0

Totally stuck saving and restoring instance state of an activity. Here is what I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register1);

    context = this;

    input_first_name = (EditText) findViewById(R.id.first_name);
    input_last_name = (EditText) findViewById(R.id.last_name);
    input_email = (EditText) findViewById(R.id.register_email);
    input_password = (EditText) findViewById(R.id.register_password);
    input_cell = (EditText) findViewById(R.id.register_cell);

    if (savedInstanceState != null) {
        System.err.println(savedInstanceState.getString("first_name"));
        input_first_name.setText(savedInstanceState.getString("first_name"));
        input_last_name.setText(savedInstanceState.getString("last_name"));
        input_email.setText(savedInstanceState.getString("email"));
        input_password.setText(savedInstanceState.getString("password"));
        input_cell.setText(savedInstanceState.getString("cell"));
    }
    ...
}

protected void onSaveInstanceState(Bundle b) {
    super.onSaveInstanceState(b);
    System.err.println("save called");

    b.putString("first_name", first_name);
    b.putString("last_name", last_name );
    b.putString("email", email);
    b.putString("password", password);
    b.putString("cell", cell);
}

protected void onRestoreInstanceState(Bundle b) {
    System.err.println(b.getString("first_name"));
    input_first_name.setText(b.getString("first_name"));
    input_last_name.setText(b.getString("last_name"));
    input_email.setText(b.getString("email"));
    input_password.setText(b.getString("password"));
    input_cell.setText(b.getString("cell"));
}

I'm getting output for "save called", so I think that my bundle was saved correctly. However, I'm never seeing any output when I come back into the activity. Anyone see what I am doing wrong? Thanks!

Pete B.
  • 3,188
  • 6
  • 25
  • 38
AndroidDev
  • 20,466
  • 42
  • 148
  • 239
  • 1
    Well, first of all you should be using the safer "get" methods that specify a default value (saveState.getString("abc", "default")). – samus Aug 14 '13 at 19:55
  • 1
    You are not setting the values of your variable (first_name, ...) anywhere – Mohamed_AbdAllah Aug 14 '13 at 19:57
  • The method `onRestoreInstanceState` will be called only when the Activity will be recreated. How you emulating destroying of Activity? – nfirex Aug 14 '13 at 19:58
  • Also, using `if (savedInstanceState != null)` to retrieve Bundle values in `onCreate(Bundle)` _and then again_ in `onRestoreInstanceState(Bundle)` is system abuse. – Vikram Aug 14 '13 at 19:59
  • @Mohamed_AbdAllah Sorry, those get set in an onclick listener that calls the next intent. Just trying to keep the amount of code to a minimum, but you're right, I didn't post that. They're set, however. – AndroidDev Aug 14 '13 at 20:02
  • Are you expecting to see these values after you kill your activity and start it again? A straightforward way to check if your `onSaveInstanceState(Bundle)` is working as intented: Rotate your device from portrait to landscape(or vice versa). If the values are preserved, then `onSaveInstanceState(Bundle)` is serving its intended purpose. – Vikram Aug 14 '13 at 20:14
  • Good idea. From portrait to landscape, all is fine. Back to portrait crashes however. – AndroidDev Aug 14 '13 at 20:19

2 Answers2

0

I really don't see anything wrong Add @Override to make sure it calls that function in your class and not the one in the super class

Like follows:

@Override
protected void onSaveInstanceState(Bundle b) {
super.onSaveInstanceState(b);
System.err.println("save called");

b.putString("first_name", first_name);
b.putString("last_name", last_name );
b.putString("email", email);
b.putString("password", password);
b.putString("cell", cell);
}
Mouhamed Ndiaye
  • 1,390
  • 2
  • 13
  • 21
0

Try this solution to be sure that you use onRestoreInstanceState correctly.

Community
  • 1
  • 1
nfirex
  • 1,523
  • 18
  • 24