且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过(弹出)菜单退出应用程序时泄漏的窗口

更新时间:2022-05-28 01:17:59

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

Try calling closeOptionsMenu() before calling finish():

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