1

I have two different activities, in the first one I enter some info into some EditTexts, I then go to the second activity, but when I return, the text that was on EditTexts in the first activity are gone.

Here is the OnCreate() for the first activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_frm_recetas);

    txtClient = (EditText) findViewById(R.id.txtNombreCliente);

           if(savedInstanceState != null){
                 String client = savedInstanceState.getString("Client");

                 txtClient.setText(client);
            }
}

I'm using the onSaveInstanceState method to save the info

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    String x = txtClient.getText().toString();
    savedInstanceState.putString("Client", x);

    super.onSaveInstanceState(savedInstanceState);
}

When doing a debug, I can see the savedInstanceState Bundle has indeed been filled in the onSaveInstanceState method, but in the OnCreate in shows null.

Maybe I have to add something in the second activity? I currently don't have anything in there other than a button that takes me back to the first activity.

Oscar
  • 53
  • 2
  • 8
  • What happens if you call `super.onSaveInstanceState(savedInstanceState)` as the first line in your `onSavedInstanceState` method? – Phil Feb 05 '14 at 16:53
  • "the text that was on EditTexts in the first activity are gone" -- the only way that would occur is if either you clear them, if you replace them with new instances, or if the first activity was destroyed for some reason. You can determine the latter case by seeing if `onDestroy()` was called. – CommonsWare Feb 05 '14 at 17:00
  • @CommonsWare..how to detect if onDestroy() was called .? – Waqar Ahmed Feb 05 '14 at 17:25

4 Answers4

0

You need to call super.onSaveInstanceState(savedInstanceState); as the first line in onSaveInstanceState method.

Next you need to put

if(savedInstanceState != null) {
             String client = savedInstanceState.getString("Client");

             txtClient.setText(client);
  }

into the onRestoreInstanceState method. onCreate is only called when re-drawing the app.

  • Already added the onRestoreInstanceState method, it didn't worked, but after doing a debug it looks like the program never uses the method (even with @Override) – Oscar Feb 05 '14 at 17:06
0

It shows null because onCreate is called only when that activity is first created, when you return back from the second activity, it resumes your first activity..try showing it on onResume() method

Jerry Wattre
  • 204
  • 2
  • 8
0

My guess is that the first activity hasn't been destroyed, so onCreate is never called when you navigate back to first activity

Lowkey
  • 836
  • 5
  • 12
-1

I guess onCreate is called when aactivity is going to load into memory, and at that time activity does not contain any thing . thats why it always return null.

to save data you have onsaveinstance method and to restore you have onrestoreinstance method.

onSaveInstanceState() is a method used to store data before pausing the activity.

onRestoreInstanceState() is method used to retrieve that data back.

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45