简体   繁体   English

复选框在重新打开应用程序时传递动作

[英]Checkbox passing of action on reopening of the app

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(ie check=shows & uncheck= hides) working correctly. 外植 :单击复选框时,处于不同活动状态的按钮可以隐藏/显示(即,check = shows&uncheck = hides)可以正常工作。
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 我试图通过提供if-else条件来实现onStart()以传递数据

@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);

            }

Instead of sending 'isCheckedValue' in 'onBubbleClickMethod' try this - 与其在“ onBubbleClickMethod”中发送“ isCheckedValue”,请尝试执行以下操作-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM