私のアクティビティにはスイッチボタンがあります。私は、アプリケーションがバックグラウンドから閉じると、スイッチボタンの状態を維持したかったのです。アプリケーションが閉じられているときに共有環境設定でスイッチボタンの状態を保存する
スイッチの状態は、アプリケーションがバックグラウンドにあるときに残りますが、アプリがバックグラウンドからクリアされるとデフォルト(OFF)状態に戻ります。
私は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()を試してみましたが、まだ状態が保存されていません。このため、アプリケーションを再び開くと、スイッチはデフォルト状態(OFF)に戻ります。 –
@Suhail Parvezさんは、アプリスイッチをオフにする前に言ったことはありませんでしたか?私はそれを再び開くと、それはonmode.am iになります。 – Vishwa
@Vishwaその反対に、私はOnモードにして、背景に戻って再び来る。オフモードになります。 –