简体   繁体   English

当按下后退按钮或使用SharedPreferences和onCheckChangeListener退出应用程序时,保存选中的按钮状态

[英]Saving the checked button state when back button pressed or app exited with SharedPreferences and onCheckChangeListener

I am trying to make an android app that allows users to block apps for a specific period of time. 我正在尝试制作一个允许用户在特定时间段内阻止应用程序的Android应用程序。 So I have a listview of installed apps in a fragment with a switch button next to it. 因此,我在一个片段中有一个已安装应用程序的列表视图,旁边有一个开关按钮。

I want it to stay checked when the user checks its or unchecks it after they press back and exit the app. 我希望它在用户按回退并退出应用程序后对其进行选中或取消选中时保持选中状态。

在此处输入图片说明

I am trying to achieve this using setOnCheckedChangeListener and Shared Preferences; 我正在尝试使用setOnCheckedChangeListener和Shared Preferences实现此目的; however; 然而; I am having trouble saving the button state in my BaseAdapter class. 我在将按钮状态保存在BaseAdapter类中时遇到麻烦。

    holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (holder.ck1.isChecked()) {
                //itemChecked[position] = true;
                b = true;
                holder.ck1.setChecked(b);
                Log.i("This is", " checked: " + position);
                SharedPreferences.Editor editor = context.getSharedPreferences("com.ibc.android.demo.appslist.app", Context.MODE_PRIVATE).edit();
                editor.putBoolean("checkBox1", b);
                editor.commit();

            } else {
                //itemChecked[position] = false;
                b= false;
                holder.ck1.setChecked(b);
                Log.i("This is", " not checked: " + position);
                SharedPreferences.Editor editor = context.getSharedPreferences("com.ibc.android.demo.appslist.app", Context.MODE_PRIVATE).edit();
                editor.putBoolean("checkBox1", b);
                editor.commit();

            }
        }

    });
    sharedPrefs = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE);
    holder.ck1.setChecked(sharedPrefs.getBoolean("checkBox1",false));

    // I think that maybe instead of false I should put the boolean b I defined in the method but I am not sure how to get it .


    return convertView;

}

How I do I modify this to reach to desired result? 我如何修改它以达到期望的结果?

I think whenever the activity created it automatically calls onCheckedChange and your SharedPreferences overwrite by default values. 我认为,每当活动创建时,它都会自动调用onCheckedChange,并且您的SharedPreferences将被默认值覆盖。 so I think you can use onStop to update the SharedPreferences. 所以我认为您可以使用onStop来更新SharedPreferences。

holder.ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) 
        { 
                SharedPreferences.Editor editor = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE).edit();
                editor.putBoolean(pakagename, b);
                editor.commit();
        }

});

sharedPrefs = context.getSharedPreferences("PACKAGE_NAME", Context.MODE_PRIVATE);
holder.ck1.setChecked(sharedPrefs.getBoolean(pakagename,false));
return convertView;

}

pakagename holds the package name of that particular listitem. pakagename保存该特定列表项的软件包名称。 Otherwise if you are targeting above api level 11, you can use a set in sharedpreferences to store all the checked package names. 否则,如果您的目标是api级别11以上,则可以在sharedpreferences中使用一组来存储所有已检查的程序包名称。 This is the easy way. 这是简单的方法。 Otherwise you can save the list of checked applications in a database. 否则,您可以将已检查的应用程序列表保存在数据库中。

I have achieved the requested feature. 我已经达到要求的功能。 Check my code: 检查我的代码:

final SharedPreferences sharedPreferences = getSharedPreferences("conditionCheck", MODE_PRIVATE); 
switchCompat.setChecked(sharedPreferences.getBoolean("switchCondition",false));        
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        final SharedPreferences sharedPreferences = getSharedPreferences("conditionCheck", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (switchCompat.isChecked()) {
            Toast.makeText(getApplicationContext(), "Checked Condition True", Toast.LENGTH_LONG).show();
            editor.putBoolean("switchCondition",switchCompat.isChecked());
            editor.commit();    
        } else {
            Toast.makeText(getApplicationContext(), "Checked Condition False", Toast.LENGTH_LONG).show();
            editor.putBoolean("switchCondition",switchCompat.isChecked());
            editor.commit();
        }
    }
});

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

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