1

Below is my alertDialog that I am using to display information to the user.

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

alertDialog.show();
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setTitle("Title");
alertDialog.setMessage("dialog");

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

    public void onClick(DialogInterface dialog, int which) {

Now, I have been doing research and looking around on stackoverflow to see how can I get this dialog to display once and I found something that could help. I had studied this code and broke it down to the limits of my knowledge in this area but I can not seem to figure out what the problems are.

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

        if (disappear == false) {

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

            alertDialog.setIcon(R.drawable.ic_launcher);
            alertDialog.setTitle("Title");
            alertDialog.setMessage("dialog");

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

                public void onClick(DialogInterface dialog, int which) {
                settings.edit().putBoolean("installed", true).commit();
    }
alertDialog.show();

The problem is that when I launch this application on an emulator my dialog doesn't show and nothing in my application respond to anything, really similar to this guy problem in this link that I found Shared Prefence for alert dialog is making my application non responsive I have to tried to break down this code and try to figure out why this is happening but Im limit in my knowledge in this area. So can somebody help solve this.

10-13 03:10:37.496: E/AndroidRuntime(761): FATAL EXCEPTION: main
10-13 03:10:37.496: E/AndroidRuntime(761): android.app.SuperNotCalledException: Activity {com.theproblemsolver/com.theproblemsolver.MainActivity} did not call through to super.onPause()
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.Activity.performPause(Activity.java:5210)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1226)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3002)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2971)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2949)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.access$800(ActivityThread.java:141)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.os.Looper.loop(Looper.java:137)
10-13 03:10:37.496: E/AndroidRuntime(761):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-13 03:10:37.496: E/AndroidRuntime(761):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 03:10:37.496: E/AndroidRuntime(761):  at java.lang.reflect.Method.invoke(Method.java:511)
10-13 03:10:37.496: E/AndroidRuntime(761):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-13 03:10:37.496: E/AndroidRuntime(761):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-13 03:10:37.496: E/AndroidRuntime(761):  at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
  • Have you tried putting `alertDialog.show()` after the other code (setting title and message)? Also, have you verified that `disappear` is `false` at that point? – codeMagic Oct 11 '13 at 01:05

2 Answers2

0

You have missed two things

                // creation of alert dialog
                AlertDialog alert = alertDialog .create();

                // show it
                alert.show();

write both things at the bottom.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • i have gotten my dialog to display now thanks to you but when I launch my emulator again it doesn't show and my application still doesn't respond to anything – Creative Mind Oct 11 '13 at 18:45
  • I guess there is some problem in shared preferences code, post your full code. – Jitender Dev Oct 12 '13 at 03:19
  • I just posted my full code for the dialog in the bottom half of my question let me know if you need me to post the whole activity – Creative Mind Oct 12 '13 at 14:22
0

This is your code modified and work

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

    if (disappear == false) {

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

        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.setTitle("Title");
        alertDialog.setMessage("dialog");

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

            public void onClick(DialogInterface dialog, int which) {
            settings.edit().putBoolean("installed", true).commit();
}
 //////////////////////
 AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.show();
wSakly
  • 387
  • 1
  • 10
  • My dialog is already displaying but I have another problem and it is when I close my emulator and launch my application on the emulator again, the dialog doesn't display and my application doesn't respond to anything. Do you think this code can solve that problem – Creative Mind Oct 12 '13 at 15:10
  • i tnink your probleme is in SharedPreferences – wSakly Oct 12 '13 at 15:32
  • this the right responce @Override public void onPause() { if (dialog != null && dialog.isShowing()) { dialog.dismiss(); super.onPause(); } } you need to ovveride the onPause and cancel your dialogue when your activity is paused – wSakly Oct 12 '13 at 16:07
  • after the user install this application to their device and after the First time launching this application a dialog will pop one time and one time only unless the user deletes the application and installs it again – Creative Mind Oct 12 '13 at 16:11
  • Whenever I press the home button or when my application leaves the activity where my dialog is in, my application crashes because of the `onPause` I will upload logcat into question – Creative Mind Oct 13 '13 at 03:55