0

Here is my GamePlay Activity code

public class GamePlay extends Activity implements OnClickListener {

private boolean disableSound = false;
//.....
//Code Code
//.....

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //.....
    //Code Code
    //.....
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putBoolean("disableSound", disableSound);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  disableSound = savedInstanceState.getBoolean("disableSound");
  Menu menu = (Menu)findViewById(R.menu.tic_tac_toe);
  MenuItem toggleSoundItemMenu = menu.findItem(R.id.toogle_sound_menu);
  if(disableSound)
      toggleSoundItemMenu.setTitle(R.string.toggle_sound_off_label);
  else
      toggleSoundItemMenu.setTitle(R.string.toggle_sound_on_label);

}

//other functions and code
}

Now on game restart I am restarting the activity. Following code is inside the onClickListener() withing appropriate case

    case R.id.game_play_restart_button:
        Intent restartActivity = new Intent(this,GamePlay.class);
        finish();
        startActivity(restartActivity);
        break;

But still the state does not persist. I disable the sound and restart the game then sound turns back on which is the default behavior. What am I missing? Any suggestion is appreciated.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • I believe this is expected. Upon restart, your activity will be destroyed and recreated. It will not persist the state. – Madhur Ahuja Nov 13 '13 at 11:01

1 Answers1

1

The savedInstanceState bundle is kept by the system as long as the activity hasn't been destroyed by the system. When you call finish, you destroy the current activity, and the bundle that comes with it. That's the reason why you can't get your boolean back.

You should consider passing this boolean as an extra in the intent like:

restartActivity.putExtra("disableSound", disableSound)

And then on the onCreate of your activity:

getIntent().getBooleanExtra("disableSound", false)

Please note that the last parameter false is just a default value. You can set it to true if that's the behaviour you want.

k3v1n4ud3
  • 2,904
  • 1
  • 15
  • 19
  • Does the name-value pair persists if activity is destroyed by the method you have mentioned? – Aniket Thakur Nov 13 '13 at 11:26
  • I'm not sure that I understand the question perfectly. The thing is the value you put inside the intent is kept by the intent while the activity is destroyed and the new one is launched. This means that it will still be available once the new activity has started – k3v1n4ud3 Nov 13 '13 at 11:30
  • I get your point we put these values in the intent we are using to start a new activity. And this value persists in the new activity for which intent was created while the current(old activity) is destroyed. – Aniket Thakur Nov 13 '13 at 11:33
  • Yes, the value persists inside the intent. That's why you need to call getIntent().getBooleanExtra, this will retrieve the intent you used to launch the new activity. – k3v1n4ud3 Nov 13 '13 at 11:35