退出应用程序(弹出)菜单

时间:2016-04-17 12:21:51

标签: android drop-down-menu menu

我希望你能帮助我,只要我通过工具栏中的(弹出)菜单选项退出我的应用程序,我就会有一个泄露的窗口。 如果我通过正常方法退出应用程序,一切都很好。

有人可能会帮忙吗?我似乎无法找到错误,我正在努力找到一个合适的解决方案。 我声明了2个静态字符串变量,但这不是我猜的问题。

提前很多。

以下是菜单的相关代码:

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 ,通过在关闭应用时添加缓慢的延迟,我解决了。显然,在退出应用程序之前,菜单窗口没有机会正确关闭,触发泄漏的原因是什么。 可能有一种更方便的方式,但这有效。

//
case R.id.exit:

       Handler handlerClose = new Handler();
       handlerClose.postDelayed(new Runnable() {
              public void run() {
                  finish();
                 }
              },300);
       return true;
//

1 个答案:

答案 0 :(得分:1)

在致电closeOptionsMenu()之前尝试拨打finish()

closeOptionsMenu();
finish();

这对我有用,但也许会有某种race condition给你带来麻烦。如果是这样,那么应该改为:

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