简体   繁体   English

无法结束我的android应用程序

[英]Can't end my android application

I'm currently getting into android development. 我目前正在进入android开发。 I try to let the user end the application by a menu button. 我尝试让用户通过菜单按钮结束应用程序。 This is my attempt: 这是我的尝试:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_exit) {
        android.os.Process.killProcess(android.os.Process.myPid());
        super.onDestroy();
    }

    return super.onOptionsItemSelected(item);
}

The application window closes, but when I switch to the "open applications window" it is still there. 应用程序窗口关闭,但是当我切换到“打开的应用程序窗口”时,它仍然存在。 How can I end/kill/close/shutdown my application completely? 如何完全结束/杀死/关闭/关闭我的应用程序?

You should think about closing your app twice, since it is not the way Android apps usually work. 您应该考虑两次关闭应用程序,因为这不是Android应用程序通常的工作方式。 Instead of this you should use the code as follow: 代替此,您应使用以下代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_exit) {
        finish()
    }

    return super.onOptionsItemSelected(item);
}

If you really want to exit your application manually, please check out the accepted answer from the following post: How to close Android application? 如果您确实要手动退出应用程序,请查看以下帖子中的可接受答案: 如何关闭Android应用程序?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM