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.
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.
You can use dialog. setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog. Window window = this.
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.
Override onDestroy
, there, check whether the dialog is present, if so, dismiss it.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With