Checkbox passing of action on reopening of the app

Deadly 提交于 2019-12-25 06:45:12

问题


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:


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

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



来源:https://stackoverflow.com/questions/39060962/checkbox-passing-of-action-on-reopening-of-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!