Xeridea
Xeridea

Reputation: 1136

Window leaked when using system overlay

I am using a system overlay to show a notification icons when user needs to be alerted app-wide of an issue. When activity is destroyed, I get a leaked window, though I am removing the view.

The code is in a base activity that is extended by most all other activities in the app.

protected void createOverlay(){
    if (overlayCreated) return;

    // Create System overlay video
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.FILL_PARENT, 150,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.BOTTOM;

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    mOverlay = (LinearLayout) inflater.inflate(R.layout.overlay_notification, null);
    mOverlayImageView = (ImageView) mOverlay.findViewById(R.id.overlay_notification_image);

    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mOverlay, params);

    final Context context = this;

    mOverlay.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(context, "WootHa!", Toast.LENGTH_SHORT).show();
            return true;
        }
    });
}

Destroy

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

    // Remove view from WindowManager
    if (mOverlay != null) {
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.removeView(mOverlay);

        mOverlay = null;
        mOverlayImageView = null;
    }
}

Error:

26576-26576/com.letstruck.mygauges E/WindowManager﹕ android.view.WindowLeaked: Activity com.letstruck.mygauges.ui.activity.fuel.TruckListActivity has leaked window android.widget.LinearLayout{431c4220 V.E..... ......I. 0,0-1080,150} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:457)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:267)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at com.letstruck.mygauges.ui.activity.LoggedInActivity.createOverlay(LoggedInActivity.java:125)
            at com.letstruck.mygauges.ui.activity.LoggedInActivity.onOptionsItemSelectedMain(LoggedInActivity.java:186)
            at com.letstruck.mygauges.ui.activity.fuel.TruckListActivity.onOptionsItemSelected(TruckListActivity.java:100)
            at android.app.Activity.onMenuItemSelected(Activity.java:2687)
            at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:373)
            at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1103)
            at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
            at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
            at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
            at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:177)
            at android.widget.AdapterView.performItemClick(AdapterView.java:308)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1483)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3485)
            at android.widget.AbsListView$3.run(AbsListView.java:4843)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

Upvotes: 3

Views: 1013

Answers (1)

popo
popo

Reputation: 445

I had the same issue. In my case, I used the wm.removeViewImmediate(View) instead of the removeView(View) which solved my problem. Maybe you can try it.

Upvotes: 5

Related Questions