내 활동에는 전환 버튼이 있습니다. 앱이 백그라운드에서 닫히면 전환 버튼 상태를 유지하고 싶었습니다.응용 프로그램이 닫힌 상태에서 공유 환경 설정을 통해 스위치 버튼 상태 저장
스위치 상태는 앱이 배경에있을 때까지 유지되지만 앱이 배경에서 삭제되면 다시 기본 (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로 바뀝니다. – Vishwa
@Vishwa 반대쪽에서 On 모드로 유지하고 배경을 찾아 다시 방문하십시오. 꺼짐 모드입니다. –