motxn motxn - 2 months ago 15
Android Question

Android app exits before exit confirmation dialog is shown

I am trying to show a confirmation alert dialog on my app's main activity when the user wants to exit. I found several answers suggesting to override

onBackPressed()
method. Unfortunately the application exits before showing the dialog and it throws an error.
Here is the code:

@Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}


Here is the error:

E/WindowManager: Activity com.example.MyApplication.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419c6eb8 that was originally added here
android.view.WindowLeaked: Activity com.example.MyApplication.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419c6eb8 that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:418)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:322)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
at android.view.Window$LocalWindowManager.addView(Window.java:559)
at android.app.Dialog.show(Dialog.java:300)
at com.example.MyApplication.MainActivity.onBackPressed(MainActivity.java:185)
at android.app.Activity.onKeyUp(Activity.java:2192)
at android.view.KeyEvent.dispatch(KeyEvent.java:2743)
at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:524)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:241)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2071)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3964)
at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3912)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3047)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:174)
at android.app.ActivityThread.main(ActivityThread.java:4952)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)


The following answer says that I am trying to show a Dialog after I've exited an Activity:

Activity has leaked window that was originally added

But as per the following suggestions that should not happen.



I am confused. Any ideas?

Answer

Simply remove super.onBackPressed();

We should remove super call while we are implimenting our own login in overrided method.

So now your code should be

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure you want to exit?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.this.finish();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}
Comments