【发布时间】:2021-06-17 14:13:07
【问题描述】:
//NotificationPermission Dialog
private void notificationPermissionDialog(){
AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setCancelable(false)
.setMessage("Do you want to Enable Notifications?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
savePermissionInPreferences("Yes");
dialogInterface.dismiss();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
savePermissionInPreferences("No");
dialogInterface.dismiss();
}
}).show();
}
//Save Notification Permission in Preferences
private void savePermissionInPreferences(String isNotificationAllowed){
SharedPreferences sharedPreferences;
sharedPreferences = getContext().getSharedPreferences("PermissionPref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("isNotificationAllowed", isNotificationAllowed);
editor.apply();
}
我创建了一个对话框,在打开片段时出现,如果我点击否,它的值会保存在共享首选项中以防止它再次出现,但是当我在同一活动中替换我的片段时,对话框会一次又一次地出现.请任何人都可以指导我完成它。 提前致谢。
【问题讨论】:
标签: android-studio android-fragments android-activity sharedpreferences android-alertdialog