androidtogglebuttonischecked

Android ToggleButton toggle state issue


I am new to android. I am creating a app which uses a toggle button. I want the toggle button to do some tasks when the button is in checked state and do some another tasks when the button is unchecked. And I want the toggle button to retain its state even when the user closes the app(by pressing back switch) and comes back. I managed to get it done by using shared preference, but the problem is that when the user turns the toggle button on and goes back and come back again, the tasks are getting done again. Is there any way to retain my toggle button on state, and not to the tasks associated with it?? And sorry for my bad English :)

public class MainActivity extends AppCompatActivity {
 ToggleButton onoff;
 public static Bundle bundle=new Bundle();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onoff = (ToggleButton) findViewById(R.id.toggleButton);

    onoff.setChecked(false);

    onoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if (isChecked) {

                // do some tasks here
              } else {

                //do some tasks here
               }
            }

    });
}


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

    @Override
    public void onResume(){
    super.onResume();
    if(bundle.getBoolean("ToggleButtonState",false))
    {
        onoff.setChecked(true);
    }

    }

   }``

Solution

  • Simply add another condition to your OnCheckedChangeListener:

    onoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if (isChecked && !bundle.getBoolean("ToggleButtonState",false) {
    
                // do some tasks here
              } else if(!isChecked){ //optional + , your preference on what to to :)
    
                //do some tasks here
               }
            }
    
    });
    

    So, when you return, and your button is checked, but your saved boolean is also true, it will not execute your task.

    Edit:
    For use with onSaveInstanceState :

    In your activity, add the following method:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
         outState.putBoolean("ToggleButtonState", onoff.isChecked());
    
        super.onSaveInstanceState(outState);
    }
    

    And retrieve this boolean in onCreate(), under findViewById(...):

    boolean checkedStatus = savedInstanceState.getBoolean("ToggleButtonState", false);
    onoff.setChecked(checkedStatus);
    

    For additional Information visit Recreating an Activity