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?