【问题标题】:How to prevent alert dialog from appearing when some condition is fulfilled?满足某些条件时如何防止出现警报对话框?
【发布时间】: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


    【解决方案1】:

    所以你可以做下面的事情

    private void notificationPermissionDialog(){
    
    SharedPreferences sharedPreferences = getSharedPreferences("PermissionPref", Context.MODE_PRIVATE);//Add these
    String permission = sharedPreferences.getString("isNotificationAllowed", "");
    
    //Add this line
    if(!permission == "No"){ 
        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();
    }
    

    如果用户对警报对话框选择“否”,它将不会再次出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      相关资源
      最近更新 更多