Search code examples
androidcheckboxcheckedunchecked

How to checkbox checked always even after app relaunch until user uncheck it in android?


My question is simple i want to checked checkbox for always even app is closed and relaunch. This process should continue till user itself uncheck it. If user itself uncheck it then should be uncheck till user checked it again in android .

Please guide me how can I do this.


Solution

  • Use this one simple code ......

    Use SharedPreferences to maintain state of check boxes.

    Initialization....

    checkBox.setChecked(getSharedPreferences("MyAPP", Context.MODE_PRIVATE).getBoolean("checkBox", true));
    

    OnCheckedChangeListener

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             getSharedPreferences("MyAPP", Context.MODE_PRIVATE).edit().putBoolean("checkBox", isChecked).commit();
    
                  }
            });
    

    First time all check boxes will be checked and it will be work like you want....

    Above code only for one check box. Try same for other check-boxes. Just change unique key for all check boxes like i used here "checkbox" in checkBox.setChecked(getSharedPreferences("MyAPP", Context.MODE_PRIVATE).getBoolean("checkBox", true));

    hope your work done by this.....