jeudi 10 août 2017

Android Studio - How do I remain the status of multiple checkboxes even after leaving the activity?

I really need some help. Even after search for hours and reading topics about the »SharedPreference«-Methode I wasn't able to solve my problem.

I want that the checkboxes (multiple checkboxes !) stay checked/unchecked even if I leave the activity. And if I go back to the original activity, the checkboxes should be in the same status as they were before. So that you can basically put the app into the background and if you bring it to the foreground again or go to another activity, the checkboxes should still be checked/unchecked (depending on what the user checked).

Thank you in advance for your help!

Here is my Code for the Activity.java:

public class TennisActivity extends AppCompatActivity {
//Variabeln
CheckBox cb118;
CheckBox cb119;
CheckBox cb120;
CheckBox cb121;
CheckBox cb122;
CheckBox cb123;
CheckBox cb149;
CheckBox cb150;
CheckBox cb151;
CheckBox cb152;
CheckBox cb153;

//Going back to menu by pressing back on device
@Override
public void onBackPressed() {
    //super.onBackPressed();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent homeIntent = new Intent(TennisActivity.this, SportActivity.class);
            startActivity(homeIntent);
            finish();
        }
    }, 1);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_tennis);

    //Variabeln initalisiern
    cb118 = (CheckBox) findViewById(R.id.checkBox118);
    cb119 = (CheckBox) findViewById(R.id.checkBox119);
    cb120 = (CheckBox) findViewById(R.id.checkBox120);
    cb121 = (CheckBox) findViewById(R.id.checkBox121);
    cb122 = (CheckBox) findViewById(R.id.checkBox122);
    cb123 = (CheckBox) findViewById(R.id.checkBox123);
    cb149 = (CheckBox) findViewById(R.id.checkBox149);
    cb150 = (CheckBox) findViewById(R.id.checkBox150);
    cb151 = (CheckBox) findViewById(R.id.checkBox151);
    cb152 = (CheckBox) findViewById(R.id.checkBox152);
    cb153 = (CheckBox) findViewById(R.id.checkBox153);
}  }




Aucun commentaire:

Enregistrer un commentaire