0

I have another problem I ran into with my alertDialog, when I force close my application on the emulator or on a device, and when I go to the apps on that device or emulator to launch my application again my alertDialog shows. I had a similar problem to this not to long ago in this link Shared Prefence for alert dialog is making my application non responsive and I thought all my issues where solved once and for all. So can someone please assist me with this issue.

    final SharedPreferences settings = getSharedPreferences("pref_name", 0);
    ("installed", false);

        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setTitle("Title");
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setAdapter(new MyAdapter(), null);

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean("installed", true);
                editor.commit();

            }
        });

        alertDialog.show();

Please let me know if you need me to elaborate more

Community
  • 1
  • 1
Inman Douche
  • 139
  • 3
  • 8
  • Where is this code? It could definitely show an `AlertDialog`. Elaborate on *why* it shouldn't show. From your other question, it looks like it will show if `installed` is `false`. Should it not be `false` at this point and if so why not? – codeMagic Sep 26 '13 at 23:45
  • 1
    Grab the logs of the crash. The Exception stacktrace is the key to find it out. – Alécio Carvalho Sep 26 '13 at 23:53
  • @Alécio my application isn't cashing, I just want to know how can I prevent my dialog from showing after the user force closes the application on a device – Inman Douche Sep 27 '13 at 19:08
  • @Alécio do you how I can achieve this? – Inman Douche Sep 28 '13 at 00:55
  • When your application is "closed" and when you launch the app again and you see the dialog again, it's probably because you're putting the code to show the dialog when the activity is launched. What do you really want? Elaborate it better, do you want to show the dialog only one-time? or always? or when? – Alécio Carvalho Sep 28 '13 at 23:40
  • @Alécio yes, I want my dialog to display one time and one time only – Inman Douche Sep 29 '13 at 04:03

1 Answers1

0
final SharedPreferences settings = getSharedPreferences("pref_name", 0);
boolean dialogAlreadyShown = settings.getBoolean("installed", false);

if (dialogAlreadyShown == false) {
   showTheDialogYouWannaShow();
   settings.edit().putBoolean("installed", true).commit();
}

like this, next time this code runs, the dialog will not be displayed...you can also save the setting on the onClick of the Dialog, but then if the user do not click on the button on the dialog, the dialog will be shown potentially multiple times, until the user presses the button, which can also be OK, depending on your requirement.

Note: in the showTheDialogYouWannaShow() i assume you are putting the code to build the alert dialog already presented in the question.

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • When I try this, I get the same result as in my application doesn't respond to anything after my first time launching the application. The same problem I had in my older question that is in the link. – Inman Douche Sep 30 '13 at 19:46