When I try to save a boolean
to a Bundle
, it appears to save (from Print Outs), but then the system does not invoke the onRestoreInstanceState
method during the process of recreation after returning from visiting the settings page of the app. (The settings are in a separate activity, tapped on from the options list in the upper right-hand corner)
According to:
https://developer.android.com/training/basics/activity-lifecycle/recreating.html
The onRestoreInstanceState
method will only be invoked if the Bundle
!= null
.
I also put it into the onCreate
method (with a null checker) and the Bundle
always turns up null.
I don't have enough reputation to post images, but this link shows the logcat: https://www.dropbox.com/s/v6vw9ynw5az5zhg/AndroidLogCat.PNG?dl=0
public class MyActivity extends AppCompatActivity {
public final static String SWITCH_NAME = "com.company.name.myactivity.SWITCH";
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
System.out.println("RESTORING INSTANCE STATE");
super.onRestoreInstanceState(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
System.out.println("RECEIVED INSTANCE STATE");
}
System.out.println("test");
super.onCreate(savedInstanceState);
System.out.println("TEST");
setContentView(R.layout.activity_my);
System.out.println("TeSt");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my, menu);
return true;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
System.out.println("SAVING STEP 1: " + getSwitchValue());
savedInstanceState.putBoolean(SWITCH_NAME, getSwitchValue());
System.out.println("SAVING TEST BEFORE SUPERCLASS: " + savedInstanceState.getBoolean(SWITCH_NAME));
super.onSaveInstanceState(savedInstanceState);
System.out.println("SAVING TEST AFTER SUPERCLASS: " + savedInstanceState.getBoolean(SWITCH_NAME));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private boolean getSwitchValue() {
boolean switchValue = true;
Switch aSwitch = (Switch) findViewById(R.id.switch1);
if (aSwitch != null) switchValue = aSwitch.isChecked();
return switchValue;
}
}
Thanks!
Robbie