random4Infinity
random4Infinity

Reputation: 193

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.

Upvotes: 5

Views: 7203

Answers (3)

Xavier.S
Xavier.S

Reputation: 329

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();
}

Upvotes: 2

slezadav
slezadav

Reputation: 6141

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

Upvotes: 2

Niko
Niko

Reputation: 8153

Put the Dialog handle in a member object, then when you finish the top activities, dismiss the dialog first.

You could make this more neat by creating abstract Activity class (which all your activities extends), which dismisses possible dialog when calling finish()

Upvotes: 1

Related Questions