Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - dismissDialog does not dismiss the dialog

Tags:

android

I am using the showDialog() and dismissDialog() to show progress dialogs in my app. Moved from creating the dialog and calling show() on it to using this in order to save state when changing orientation.

But when I change the orientation from portrait->landscape->portrait, the dismissDialog() no longer dismisses the dialog. The dialog stay there all the time and I need to press the back button for it to disappear.

Any reason why it would behave that way?

Edit

To overcome this issue, I tried adding a removeDialog() in onDestroy() so that the dialog is not created/displayed twice and before orientation change, the dialog is removed. Tried adding log statements and see what happens

05-21 12:35:14.064: DEBUG/MyClass(193): *************callingShowDialog
05-21 12:35:14.064: DEBUG/MyClass(193): *************onCreareDialog

05-21 12:35:15.385: DEBUG/MyClass(193): *************onSaveInstanceState
05-21 12:35:15.415: DEBUG/MyClass(193): *************onDestroy

05-21 12:35:15.585: DEBUG/MyClass(193): *************callingShowDialog
05-21 12:35:15.585: DEBUG/MyClass(193): *************onCreareDialog
05-21 12:35:15.715: DEBUG/MyClass(193): *************onCreareDialog
05-21 12:35:17.214: DEBUG/MyClass(193): *************onSaveInstanceState
05-21 12:35:17.214: DEBUG/MyClass(193): *************onDestroy

05-21 12:35:17.275: ERROR/WindowManager(193): android.view.WindowLeaked: Activity com.android.MyClass has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@43362088 that was originally added here

05-21 12:35:17.395: DEBUG/MyClass(193): *************callingShowDialog
05-21 12:35:17.395: DEBUG/MyClass(193): *************onCreareDialog
05-21 12:35:17.475: DEBUG/MyClass(193): *************onCreareDialog

If we see here, initially when the activity is displayed, the onCreateDialog is called once and on changing the orientation, onSaveInstanceState and onDestroy are called.

But after that, onCreateDialog is called twice (once by a call to showDialog which I make, but why the 2nd time?) and this happens every time I change the orientation hence forth.

Any idea why that happens?

Thanks again

like image 350
lostInTransit Avatar asked May 21 '09 05:05

lostInTransit


People also ask

What is DialogFragment in android?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

What is setCanceledOnTouchOutside in android?

setCanceledOnTouchOutside only prevents dismissing by clicking outside of the dialog. But you can still dismiss it with back button for instance. If you don't want your dialog to be cancelable at all use dialog.setCancelable(false)

How do I dismiss Materialalertdialogbuilder?

setCancelable(false); AlertDialog dialog = builder. show(); In order to dismiss the dialog, you can call dismiss function like this.


3 Answers

The best solution for me seems to use removeDialog(id) instead of dismissDialog(); Less re-usage this way, but safer (doesn't throw anything) and no problems when changing orientation.

like image 121
Harri Avatar answered Sep 27 '22 17:09

Harri


I've run into this when showing a dialog in the onCreate override of the Activity. Try moving the code that causes the dialog to be shown into the onPostCreate override instead. This seems to work for me.

like image 32
sakamoto Avatar answered Sep 27 '22 17:09

sakamoto


I wasted over a day on a problem like this with dialogs not being dismissed when calling dismissDialog. It occurred after my first screen rotation and never worked afterwards. I had been using an external AsyncTask which was passed a referenced to the state explicitly.

In the end the solution was really simple.... removeDialog(int) instead of dismissDialog(int).

I had spent what felt like forever proving to myself I was doing the dismiss on the correct activity and checking the number of times things were called etc, but it was caused by some of the jiggerypokery behind the scenes.

like image 32
Uniqe Avatar answered Sep 27 '22 16:09

Uniqe