I hope you can help me, I have a leaked window as soon as I exit my application via the (popup) menu option in the toolbar. If I exit the app through a normal method everything is fine.
Could somebody maybe help? I cannot seem to find the error and I am trying to find a proper solution. I have 2 static String variables declared but that cannot be the problem I guess.
Many thx in advance.
Here is the relevant code for the menu:
public class LoginActivity extends AppCompatActivity {
//
private Object mState = null;
private Object HIDE_MENU = null;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
//
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// inflate menu from xml
MenuInflater mInflater = this.getMenuInflater();
mInflater.inflate(R.menu.menu_main, menu);
if (mState == HIDE_MENU) {
menu.getItem(0).setVisible(false);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
// Unassigned
return true;
case R.id.exit:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
//
}
The Logcat:
E/WindowManager: android.view.WindowLeaked: Activity myapp.example.com.myapp.LoginActivity has leaked window android.widget.PopupWindow$PopupDecorView{1f4c330 V.E...... ........ 0,0-686,168} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:368)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1258)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1110)
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.widget.ActionMenuPresenter$OpenOverflowRunnable.run(ActionMenuPresenter.java:781)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
EDIT, my menu xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="myapp.example.com.myapp.MainActivity">
<item
android:id="@+id/settings"
android:title="Instellingen"
app:showAsAction="never" />
<item
android:id="@+id/exit"
android:title="Exit"
app:showAsAction="never" />
</menu>
EDIT2, I solved it myself by adding a slow delay in closing the app. Apparently the menu window doesn`t get a chance to properly close before the app is exited, what triggers the leak. Probably there is a more convenient way but this works.
//
case R.id.exit:
Handler handlerClose = new Handler();
handlerClose.postDelayed(new Runnable() {
public void run() {
finish();
}
},300);
return true;
//
Try calling
closeOptionsMenu()
before callingfinish()
:This worked for me, but there could perhaps be some sort of race condition giving you problems. If so, this should work instead: