serg66
serg66

Reputation: 1148

AlertDialog causes leaks when activity finishes

I have a base Activity that extends FragmentActivity. In this activity I have a dialog with an OK button. The dialog says there's no internet connection. After a user clicks on the button the activity finishes. I'm aware that before exiting an activity I must call dismiss on an AlertDialog object. I tried to do that with two ways: in the onClick method of the DialogInterface.OnClickListener object

builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
            BaseActivity.this.finish();
        }
    });

After I realized the previous code didn't work I tried to make a member variable in my BaseActivity class and calling the dismiss method onDestroy:

@Override
protected void onDestroy() {
    super.onDestroy();

    if (alertDialog != null) {
        alertDialog.dismiss();
        alertDialog = null;
    }
}

It didn't work either. I still get this in my debugger:

01-07 01:43:10.815    3055-3055/com.example.app E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.app.ArticlesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{1a183701 V.E..... R....... 0,0-1026,640} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:298)
        at com.example.app.BaseActivity.showErrorDialog(BaseActivity.java:145)
        at com.example.app.BaseActivity.onCreate(BaseActivity.java:57)
        at com.example.app.ArticlesActivity.onCreate(ArticlesActivity.java:30)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Thank you in advance for your help.

Upvotes: 1

Views: 2811

Answers (3)

serg66
serg66

Reputation: 1148

I figured it out. Actually there were two dialogs. One of them I dismissed and the second one was still around, that's why I got the error. Thank you guys though, you gave me valuable information to think.

Upvotes: 1

omkar.ghaisas
omkar.ghaisas

Reputation: 245

I would declare an interface within the dialog class and let the activity implement that interface. Then set a public method within the activity, which your dialog can call and pass on teh result.

if it is determined, that you need to finish / exit the activity, rather than the dialog calling the finish directly, it would just call the public method and that method inside the activity will actually finish the activity.

Refer this link for how this is done - How to get button clicks in host fragment from dialog fragment

http://developer.android.com/training/basics/fragments/communicating.html

Upvotes: 2

Alexander
Alexander

Reputation: 48262

You would want to extend DialogFragment instead. There in OnCreateDialog() you will return your AlertDialog. You show the fragment with show() and dismiss with dismiss(). This guarantees no leaks.

Upvotes: 2

Related Questions