Checkbox passing of action on reopening of the app

2019-09-07 02:30发布

I have stored the checkbox value(check/uncheck) using shared preference,but I face problem while passing the action of checkbox on/after closing and reopening the app.
Explantion: A button in different actvity hides/shows on clicking the checkbox(i.e check=shows & uncheck= hides) working correctly.
when I close the app and reopen the checkbox stays checked but button is not appearing

checkbox code saved using shared preference

final CheckBox checkBox = (CheckBox) findViewById(R.id.add_fb);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();

        checkBox.setChecked(preferences.getBoolean("checked",false));

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheckedValue = isChecked;
                editor.putBoolean("checked", isChecked);
                editor.apply();
            }
        });

    }

I tried to implement onStart() for passing data by providing if-else condition

@Override
    protected void onStart() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        super.onStart();
        if(checkBox.isChecked()) {
            editor.putBoolean("checked", true);
            editor.apply();
        }else{
            editor.putBoolean("checked", false);
            editor.apply();
        }
    }

This is where i am passing the data once the checkbox is checked

@Override
            public void onBubbleClick(BubbleLayout bubble) {
                Intent in = new Intent(MainActivity.this, PopUpWindow.class);
                in.putExtra("yourBoolName", isCheckedValue );
                startActivity(in);

            }

1条回答
Bombasti
2楼-- · 2019-09-07 02:54

Instead of sending 'isCheckedValue' in 'onBubbleClickMethod' try this -

in.putExtra("yourBoolName",preferences.getBoolean("checked",false));

查看更多
登录 后发表回答