0

I get a crash when I use this code:

if (!savedInstanceState.containsKey("mybool")) {
    //  my code
}

My onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);}

Elsewhere in my code:

savedInstanceState.putBoolean("mybool", true);  
azizbekian
  • 60,783
  • 13
  • 169
  • 249
Jonathan Doe
  • 101
  • 5
  • 20

1 Answers1

1

savedInstanceState will be null first time activity launches.

savedInstanceState will not be null after activity is recreated (e.g. as a result of configuration change, system kill the process of your app).

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (null == savedInstanceState) {
        // activity is being launched first time
    } else {
        // is called after configuration change or framework restore your app
    }
}
azizbekian
  • 60,783
  • 13
  • 169
  • 249