Open OptionsMenu of Activity leaks window on device rotation
Asked Answered
M

1

7

Trying to understand what cause the window leak in my Activity. The activity extends AppCompatActivity. It has a single view with a RecyclerView and a ProgressBar invisible while rotating.

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    if (savedInstanceState != null) {
        chosenSorting = savedInstanceState.getString("sorting");
    }

    mRecyclerView = (RecyclerView) findViewById(R.id.rv_pop_view);
    mLoadingIndicator = (ProgressBar) findViewById(R.id.pb_loading_indicator);
    RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    moviesRecyclerView.setLayoutManager(mLayoutManager);

    mRecyclerView.setHasFixedSize(true);

    if (chosenSorting.equals("")) {
        load("A");
    } else {
        load("B");
    }
}

Everything works fine on screen rotation, no UI problem. But when the rotation happens while OptionsMenu is open there is a leaked window:

E/WindowManager: android.view.WindowLeaked: Activity com.example.MainActivity has leaked window android.widget.PopupWindow$PopupDecorView{2aeb62a V.E...... ......ID 0,0-686,336} that was originally added here
                                                                                at android.view.ViewRootImpl.<init>(ViewRootImpl.java:418)
                                                                                at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
                                                                                at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                                                                                at android.widget.PopupWindow.invokePopup(PopupWindow.java:1378)
                                                                                at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1234)
                                                                                at android.support.v7.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:105)
                                                                                at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:33)
                                                                                at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:129)
                                                                                at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:206)
                                                                                at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:721)
                                                                                at android.support.v7.view.menu.StandardMenuPopup.tryShow(StandardMenuPopup.java:160)
                                                                                at android.support.v7.view.menu.StandardMenuPopup.show(StandardMenuPopup.java:187)
                                                                                at android.support.v7.view.menu.MenuPopupHelper.showPopup(MenuPopupHelper.java:290)
                                                                                at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:175)
                                                                                at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6121)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

Is this the problem ? :

at android.support.v7.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:803)

Setting main UI elements to null and closeOptionsMenu() does not help...

@Override
protected void onDestroy() {
    closeOptionsMenu();
    mRecyclerView = null;
    mAdapter = null;
    mLoadingIndicator = null;

    super.onDestroy();
}

Question: How does the open OptionsMenu cause this window-leak on screen rotation and How to avoid it?

Mckown answered 17/4, 2017 at 15:25 Comment(1)
Two years later and I get the same issue and it does not seem anyone has found a solution. I've tried touch events, closeOptionsWindow() at various times and nothing works. The menu returns when the screen is rotated as well so it is clearly not being closed.Hatching
T
0

This same exception is thrown if and EditText view has a selection (with the pop up dialog to "copy...") and you rotate the device.

This is with targetSdkVersion 28, minSdkVerion 22 and androidx running on v28 (Pixel 2) but not when running on v22 (Nexus 7)

2019-06-12 16:08:06.392 30838-30838 E/WindowManager: android.view.WindowLeaked: Activity com.hanafey.testing.flexedit.MainActivity has leaked window android.widget.PopupWindow$PopupDecorView{2c0d944 V.E...... R....... 0,0-1029,168} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:511)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at android.widget.PopupWindow.invokePopup(PopupWindow.java:1434)
        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1203)
        at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1170)
        at com.android.internal.widget.FloatingToolbar$FloatingToolbarPopup.show(FloatingToolbar.java:598)
        at com.android.internal.widget.FloatingToolbar.doShow(FloatingToolbar.java:288)
        at com.android.internal.widget.FloatingToolbar.show(FloatingToolbar.java:218)
        at com.android.internal.view.FloatingActionMode$FloatingToolbarVisibilityHelper.updateToolbarVisibility(FloatingActionMode.java:378)
        at com.android.internal.view.FloatingActionMode$1.run(FloatingActionMode.java:67)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-06-12 16:08:06.418 30838-30838 E/MA: onCreate:wt=0 CODENAME=REL SDK_INT=28 RELEASE=9 
Themselves answered 12/6, 2019 at 23:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.