在我的活动中,我有一个切换按钮。我想在应用程序从背景关闭时保持开关按钮的状态。当应用程序关闭时通过共享首选项保存切换按钮状态
当应用程序位于后台时,交换机的状态保持不变,但当应用从后台清除时,交换机的状态会回到默认(关闭)状态。
我试着从here复制程序。但我仍然无法保持开关按钮的状态。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
switch1 = (Switch) findViewById(R.id.switch1);
SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyz", MODE_PRIVATE);
switch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
switch1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (switch1.isChecked()) {
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.apply();
switch1.setChecked(true);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
// Setting Dialog Title
alertDialog.setTitle("Download all the Product's PDF.");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.pdf_alert_dialog);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch1.setChecked(false);
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.apply();
dialog.dismiss();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("DOWNLOAD ALL ",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.apply();
AlertDialog.Builder alertDialog1 = new AlertDialog.Builder(context);
// Setting Dialog Title
alertDialog1.setTitle("Free storage Available:" + megAvailable1 + " MB");
alertDialog1.setMessage("File size to Download: POJO MB");
// Setting Icon to Dialog
alertDialog1.setIcon(R.drawable.pdf_alert_dialog);
alertDialog1.setPositiveButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
switch1.setChecked(false);
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", false);
editor.apply();
dialog1.dismiss();
}
});
// Setting Negative "NO" Button
alertDialog1.setNegativeButton("DOWNLOAD ",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog1, int which) {
getFeedDownload();
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putBoolean("NameOfThingToSave", true);
editor.apply();
}
});
alertDialog1.show();
}
});
// Showing Alert Message
alertDialog.show();
} else {
}
}
});
你能告诉我哪里出错了吗?
我也尝试使用editor.commit(),但依然状态没有得到保存,因为这一点,当我重新打开应用程序的切换返回到默认状态(灭)。 –
@Suhail Parvez做你说杀应用程序开关之前处于关闭模式。当你重新打开它它去onmode.am我 – Vishwa
@Vishwa它的对面,我保持在On模式,当我关闭应用程序从背景并再次回来。它将处于关闭模式。 –