0

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

Robbie S
  • 1
  • 2
  • "then the system does not invoke the onRestoreInstanceState method" -- what are you doing to try to cause `onRestoreInstanceState()` to be called? Are you rotating the screen, for example? – CommonsWare Sep 27 '15 at 16:43
  • I attempted to answer that in an edit just now. Is there any other information that is vague or missing? – Robbie S Sep 27 '15 at 16:52
  • Your activity is not going to be recreated just because you visited another activity within your own app. – CommonsWare Sep 27 '15 at 17:04

0 Answers0