Java – activity leaked popupwindow window after rotation
When I rotate the device while displaying PopupMenu, a windowleft error appears
That's my PopupMenu:
private void showSelectionMenu(View caller) {
popup = new PopupMenu(this,caller);
// popup.getMenuInflater().inflate(R.menu.selection_menu,popup.getMenu());
popup.inflate(R.menu.selection_menu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(FileListActivity.this,getString(R.string.selecting)+" "+item.getTitle(),Toast.LENGTH_SHORT).show();
switch (item.getItemId()) {
case R.id.select_all: mediaFolder.selectAll(); break;
case R.id.select_none: mediaFolder.selectNone(); break;
case R.id.select_videos: mediaFolder.selectVideos(); break;
case R.id.select_pictures: mediaFolder.selectImages(); break;
default: break;
}
findViewById(R.id.buttonRenameSelected).setEnabled(mediaFolder.numberSelected>0);
cla.redraw();
return true;
}
});
if (Build.VERSION.SDK_INT >= 14) {
popup.setOnDismissListener(new PopupMenu.OnDismissListener() {
@Override
public void onDismiss(PopupMenu menu) {
Log.d(TAG,"selectionMenu dismissed");
}
});
}
popup.show();
}
I know many programmers here have made the same mistake before. Usually they are told to remove the menu in ondestroy () Me too.
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG,"FileListActivity onDestroy()");
Log.d(TAG,"onDestroy: popup==null? "+(popup==null));
if (popup != null) { popup.dismiss(); popup = null; }
Log.d(TAG,"onDestroy: popup==null? "+(popup==null));
dataFragment.mRetainedCache = mMemoryCache;
dataFragment.setData(mediaFolder);
}
This is the generated log, including errors:
02-10 22:24:15.969 D: FileListActivity onDestroy()
02-10 22:24:15.969 D: onDestroy: popup==null? false
02-10 22:24:15.971 D: selectionMenu dismissed
02-10 22:24:15.971 D: onDestroy: popup==null? true
02-10 22:24:16.064 E: android.view.WindowLeaked: Activity com.myApp.FileListActivity has leaked window android.widget.PopupWindow$PopupDecorView{365b288 V.E...... ......ID 0,0-822,1152} 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:94)
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:78)
at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:30)
at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:92)
at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:171)
at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:680)
at android.support.v7.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:163)
at android.support.v7.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:129)
at android.support.v7.widget.PopupMenu.show(PopupMenu.java:216)
at com.myApp.FileListActivity.showSelectionMenu(FileListActivity.java:434)
at com.myApp.FileListActivity.access$100(FileListActivity.java:44)
at com.myApp.FileListActivity$5.onClick(FileListActivity.java:186)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
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:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
02-10 22:24:16.079 D: FileListActivity onCreate()
As you can see in the log, the PopupMenu is cancelled and set to null before the error occurs
Do you know what's wrong?
Edit:
This is where showselectionmenu (view caller) is called (in oncreate):
Button buttonSelectFiles = (Button) findViewById(R.id.buttonSelectFiles);
buttonSelectFiles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showSelectionMenu(v);
}
});
Edit 2:
I just found out I'm running Android 7.1 This error occurs on nexus 5 of 1 (lineageos, built every night), but it is running fire OS 5.3 2.1 (based on Android 5), but not on fire HD 8, instead of running Android 4.4 on Samsung Galaxy s 4(CyanogenMod 11). So maybe it only happens on Android 7, or there is an error in lineageos!? If someone has official Android 7, he / she may try to use this Android popup menu example to trigger the error You only need to add the following code:
@Override
public void onDestroy() {
super.onDestroy();
if (popup != null) { popup.dismiss(); popup = null; }
}
And make popup a class member variable
Edit 3:
This is not a lineageos problem Run Android 7.1 This error also occurs in the Android studio simulator of 1
Solution
An error occurred when your code tried to display a pop-up window The activity is recreated after the rotation changes, so I think your caller parameter contains the view of the destroyed activity You should check the source of the caller parameter and reassign it if necessary
