1
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_layout);
    toggleButton=(ToggleButton) findViewById(R.id.toggleButton1);
}
@Override
public void onSaveInstanceState(Bundle save) {
    super.onSaveInstanceState(save);
    save.putBoolean("ToggleButtonState", toggleButton.isChecked());
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    toggleButton.setChecked(savedInstanceState.getBoolean("ToggleButtonState",false);
}

It seem like it should work, but if i do the following:

  1. run my application by its icon on applications menu
  2. checking the toggle button
  3. going back to home screen by pressing the back button
  4. activating my application from its icon again

i get to see my toggle button unchecked, why is it so? and how do i overcome this?

VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103

2 Answers2

4

I missed what save and restore methods are for, but to achieve the functionality i was looking for i did the following:

public class MainActivity extends Activity {

    private ToggleButton toggleButton;
    private static Bundle bundle = new Bundle();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
    }



    @Override
    public void onPause() {
        super.onPause();
        bundle.putBoolean("ToggleButtonState", toggleButton.isChecked());
    }

    @Override
    public void onResume() {
        super.onResume();
        toggleButton.setChecked(bundle.getBoolean("ToggleButtonState",false));
    }
}
Ofek Ron
  • 8,354
  • 13
  • 55
  • 103
  • Wanna know why is it working? see answer at [when is onRestoreInstanceState called](http://stackoverflow.com/a/26190904/4188683) – Eido95 Apr 16 '17 at 13:19
0

This is my code

package com.example.stackoverflow;



import android.app.Activity;
import android.os.Bundle;

import android.widget.ToggleButton;

public class MainActivity extends Activity {
    ToggleButton toggleButton;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
    }

    @Override
    public void onSaveInstanceState(Bundle save) {
        super.onSaveInstanceState(save);
        save.putBoolean("ToggleButtonState", toggleButton.isChecked());
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        toggleButton.setChecked(savedInstanceState.getBoolean("ToggleButtonState",false));
    }
}
VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
  • let me test on my s3 ... i tested on galaxy nexus – VendettaDroid Sep 08 '12 at 23:01
  • I hope you are not pressing back button. Also, can you share your android manifest file. – VendettaDroid Sep 08 '12 at 23:03
  • I am pressing the back button, isnt it suppose to save state whenever the app gets invisible, and then restore whenever the app gets visible again? – Ofek Ron Sep 08 '12 at 23:05
  • Ok ... then there is a different problem here – VendettaDroid Sep 08 '12 at 23:06
  • Your onRestoreInstanceState is not called at all. Put a log statement in onCreate, onSave and onRestore and perform your sequence. You will see that saved is called but not onRestore. Let me find you example. – VendettaDroid Sep 08 '12 at 23:11
  • yeah youre right, i read the doc of those methods and i had them all wrong, they actually used to preserve the state when android OS kills your application and the user trying to use it at a later time – Ofek Ron Sep 08 '12 at 23:14
  • Yes ... absolutely correct. Thats the way to go. The documentation of android is perfect. – VendettaDroid Sep 08 '12 at 23:15