mardi 14 août 2018

How to make checkbox determine if activity should be displayed or not

Simple explanation:

Checkbox placed in one activity (walkthrough) should determine if this activity is shown again at second app launch or not. Checkbox checked - activity won't be displayed anymore and app launch should start with MainActivity. What is simple trick to do that? Do I need to pass checkbox state from one activity to another in case of use sharedpreferences? I do not want to use any alertbox or dialog.

Here's my sharedpreferences attempt in Preshow.class (activity):

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        if(preferences.contains("checked") && preferences.getBoolean("checked", false) == true){
            radioButton.setChecked(true);
        }else{
            radioButton.setChecked(false);
        }
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(radioButton.isChecked()){
                editor.putBoolean("isShowOnboarding", true);
                editor.apply();
            }else {
                editor.putBoolean("isShowOnboarding", false);
                editor.apply();
            }}}

Code in MainActivity:

Boolean isShowOnboarding = getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .getBoolean("isShowOnboarding", true);

    if (isShowOnboarding) {
        //show walkthrough activity
        startActivity(new Intent(MainActivity.this, Preshow.class));
        finish();
    }




Aucun commentaire:

Enregistrer un commentaire