在我的活動中,我有一個切換按鈕。我想在應用程序從背景關閉時保持開關按鈕的狀態。當應用程序關閉時通過共享首選項保存切換按鈕狀態
當應用程序位於後臺時,交換機的狀態保持不變,但當應用從後臺清除時,交換機的狀態會回到默認(關閉)狀態。
我試着從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模式,當我關閉應用程序從背景並再次回來。它將處於關閉模式。 –