Search code examples
androidsharedpreferencesandroid-alertdialograting-system

how to save which button was selected in alertdialog box so that it never shows again?


I need some help with this, as I was searching for an answer all results were unhelpful I just want straight forward code to copy and paste so that I can save what was selected and never show the dialog box again.

SharedPreferences sharedpreferences = getSharedPreferences("shared", Context.MODE_PRIVATE);
            AlertDialog.Builder b=  new  AlertDialog.Builder(this)
                    .setTitle("Review and Rate Converjz!")
                    .setMessage("Have you enjoyed Converjz? If you wouldn't mind to take a minute of your time and rate/review this app," +
                            "that would be much appreciated!")
                    .setPositiveButton("RATE/REVIEW", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.TechnologyForTomorrow.Converjz");
                                    Intent openPlayStore = new Intent(Intent.ACTION_VIEW, uri);
                                    try {
                                        startActivity(openPlayStore);
                                    } catch (ActivityNotFoundException e) {

                                    }
                                dialog.dismiss();


                                }


                            }

                    )
                    .setNegativeButton("CANCEL",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.dismiss();

                                }
                            }
                    );
            b.show();


        }

Solution

  • Use code below to store the key that user has chosen in application preferences.

    private static final String settingsKey = "MyAppSettings";
    private static final String hasUserRatedAppKey= "hasUserRatedApp";
    public static boolean hasUserRatedApp(Context act)
    {
        SharedPreferences pref = act.getSharedPreferences(settingsKey, Context.MODE_PRIVATE);
        return pref.getBoolean(hasUserRatedAppKey, false);
    }
    
    public static void setHasUserRated(Context context, boolean rated)
    {
        SharedPreferences pref = act.getSharedPreferences(settingsKey, Context.MODE_PRIVATE);
        SharedPreferences.Editor preferencesEditor = pref.edit();
        preferencesEditor.putBoolean(hasUserRatedAppKey, rated);
        preferencesEditor.commit();
    }
    

    and set the value of rated in setPositiveButton:

    .setPositiveButton("RATE/REVIEW", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    ....
                                setHasUserRated(getContext(),true);
                                dialog.dismiss();
    
    
                                }
    
    
                            }
    
                    )
                    ....
    

    Edit: I missed a part:

    before showing the alert dialog, you must check to see if app has been rated before, thats why I included hasUserRatedApp function :

    if(!hasUserRatedApp(MyActivityClass.this))
    {
        AlertDialog.Builder b= ...
    }