I guess that this is a problem that most of us have had - and many still 
have without knowing it - because it normally does not cause a crash.
It is also badly handled in the documentation.

Here are some more details on how I normally do it:

First, define an alertDialog in your Activity class:

private AlertDialog alertDialog;

In the end of the AlertDialog.Builder method, use

alertDialog = builder.show();

In the onPause() or onStop() method, use

       try {
            alertDialog.cancel();
        }
            catch (Exception e) {
             ;
        } 

Or you can use alertDialog.dismiss(); which does (almost) the same.

Regards, Terry


kl. 07:57:01 UTC+2 onsdag 18. juli 2012 skrev B.Arunkumar følgende:
>
> Hi, 
>
>      I have an app which produces more than one alert dialog in 
> different situations. Let us say we have more than one alert dialog on 
> the screen - dialog1, dialog2. 
>
> When dialog1 ok is pressed as per the code below, the screen finishes: 
>
> AlertDialog.Builder alert_box=new 
> AlertDialog.Builder(AsyncRecordTrial.this); 
>                                 alert_box.setIcon(R.drawable.icon); 
>                                 alert_box.setMessage(dialogmessage); 
>                                 alert_box.setPositiveButton("Ok",new 
> DialogInterface.OnClickListener() { 
>                                         @Override 
>                                         public void 
> onClick(DialogInterface dialog, int which) { 
>                                                 // TODO Auto-generated 
> method stub 
>                                                 dialog.cancel(); 
>                                         } 
>                                 }); 
>
>                                 alert_box.setOnCancelListener(new 
> OnCancelListener(){ 
>
>                                         @Override 
>                                         public void 
> onCancel(DialogInterface dialog) { 
>                                                 // TODO Auto-generated 
> method stub 
>
>                                                         finish(); 
>                                         } 
>                                 }); 
>                                 alert_box.show(); 
>
> However, dialog2 doesn't cancel and it leads to a memory leak in the 
> logcat with crash. 
>
>  E/WindowManager(27573): Activity 
> com.example.OnVRViewer.AsyncRecordTrial has leaked window 
> com.android.internal.policy.impl.PhoneWindow$DecorView@407b9748 that 
> was originally added here 
> E/WindowManager(27573): android.view.WindowLeaked: Activity 
> com.example.OnVRViewer.AsyncRecordTrial has leaked window 
> com.android.internal.policy.impl.PhoneWindow$DecorView@407b9748 that 
> was originally added here 
> E/WindowManager(27573):         at 
> android.view.ViewRoot.<init>(ViewRoot.java: 
> 266) 
> E/WindowManager(27573):         at 
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174) 
> E/WindowManager(27573):         at 
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117) 
> E/WindowManager(27573):         at android.view.Window 
> $LocalWindowManager.addView(Window.java:424) 
> E/WindowManager(27573):         at 
> android.app.Dialog.show(Dialog.java:241) 
> E/WindowManager(27573):         at android.app.AlertDialog 
> $Builder.show(AlertDialog.java:823) 
> E/WindowManager(27573):         at com.example.OnVRViewer.AsyncRecordTrial 
> $5.handleMessage(AsyncRecordTrial.java:1461) 
> E/WindowManager(27573):         at 
> android.os.Handler.dispatchMessage(Handler.java:99) 
> E/WindowManager(27573):         at android.os.Looper.loop(Looper.java:130) 
> E/WindowManager(27573):         at 
> android.app.ActivityThread.main(ActivityThread.java:3691) 
> E/WindowManager(27573):         at 
> java.lang.reflect.Method.invokeNative(Native Method) 
> E/WindowManager(27573):         at 
> java.lang.reflect.Method.invoke(Method.java:507) 
> E/WindowManager(27573):         at com.android.internal.os.ZygoteInit 
> $MethodAndArgsCaller.run(ZygoteInit.java:907) 
> E/WindowManager(27573):         at 
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
>
> Now how do I cancel also dialog2? 
>
> Thank you, 
> B.Arunkumar

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to