通过(弹出)菜单退出应用程序时窗口泄漏 [英] Leaked window when exit app through (popup) menu

查看:118
本文介绍了通过(弹出)菜单退出应用程序时窗口泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您能为我提供帮助,一旦我通过工具栏上的(弹出)菜单选项退出我的应用程序,就会有一个泄漏的窗口. 如果我通过常规方法退出应用,一切都很好.

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.

有人可以帮忙吗?我似乎找不到错误,并且正在尝试找到适当的解决方案. 我声明了2个静态String变量,但是我猜那不是问题.

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.

提前很多.

以下是菜单的相关代码:

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);
        }
    }
//
}

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)

编辑,我的菜单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 ,我自己解决了问题,添加了关闭应用程序的缓慢延迟.显然,在退出应用程序之前,菜单窗口没有机会正确关闭,这是导致泄漏的原因. 也许有一种更方便的方法,但这行得通.

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;
//

推荐答案

在调用finish()之前尝试调用closeOptionsMenu():

closeOptionsMenu();
finish();

这对我有用,但是可能会有某种种族条件给出你有问题.如果是这样,应该改为:

This worked for me, but there could perhaps be some sort of race condition giving you problems. If so, this should work instead:

private boolean exit = false;


@Override
public void onOptionsMenuClosed(Menu menu) {
    super.onOptionsMenuClosed(menu);

    if( exit )
        finish();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.settings:
                // Unassigned
            return true;

        case R.id.exit:
            exit = true;
            closeOptionsMenu();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

这篇关于通过(弹出)菜单退出应用程序时窗口泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆