Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing dialog on activity finish

In my app I have several activities one after the other. After my login screen I have Home screen and after that several screens. Now When user select device home button or power off button I want to display login screen when user again comes to my app and then Home screen. Rest all activity I am finishing it from my base class. Now till here I have done, My problem is when I show a dialog in some other activity and at that instance if user click on home or power button, then i am getting WINDOW LEAKED EXCEPTION.

enter image description here

Like I have TempActivity is displaying a dialog and user clicked home button so StoreActivity and TempActivity will finish but Dialog never got chance to be dismissed. So What would be the best way to deal with this situation. Is there some better way to dismiss the dialog so that I don't get any exception.

like image 266
random4Infinity Avatar asked Mar 06 '13 10:03

random4Infinity


People also ask

How do you dismiss dialog with click on outside of dialog?

You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.

How do you dismiss dialog in Kotlin?

Android AlertDialog class is used to display a dialog box to alert the user with positive and negative buttons. Positive button is used to continue with the action specified. Negative button is used to dismiss the alerted action.


2 Answers

Override onDestroy, there, check whether the dialog is present, if so, dismiss it.

like image 122
slezadav Avatar answered Sep 30 '22 16:09

slezadav


dismiss() in onDestroy() doesn't solve this problem. Try to override activity.finish() like:

@Override
public void finish() {
    if(mDialog != null) {
        mDialog.dismiss();
    }
    super.finish();
}
like image 41
Xavier.S Avatar answered Sep 30 '22 18:09

Xavier.S