System.exit(0) 没有关闭我所有的活动? [英] System.exit(0) doesnt close all my activities?

查看:19
本文介绍了System.exit(0) 没有关闭我所有的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个活动,所以活动 1 转到活动 2,然后在活动 2 上我有一个退出按钮.但是当我点击它时,它只会退出活动编号 2 并再次返回活动 1.基本上感觉就像我刚刚再次启动了应用程序.我不知道为什么?

I have 2 activity, so activity 1 go to activity 2 then on activity 2 I have an exit button. But when I click it, all it only exited the activity number 2 and return to activity 1 again. Its basically felt like I just started the application again. I am not sure why?

这是我的代码.

Button btExit = (Button) findViewById(R.id.btExit);
    btExit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
            System.exit(0);
        }
    });

推荐答案

System.exit(0);

是终止 android 应用程序的一种糟糕方式.Android在自己的os中管理

is a bad way of termination of android apps. Android manages it in its own os

您可以通过相应的 Intent 来调出 Home 应用程序:

You can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

希望能帮到你

-

那么我想你的目标是完成所有堆积的活动..

Then I suppose you are aiming at finishing all the stacked up activity..

这里是:-

关闭之前的所有活动如下:

Closing all the previous activities as follows:

Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("Exit me", true);
startActivity(intent);
finish();

然后在 MainActivity onCreate() 方法中添加这个来完成 MainActivity

Then in MainActivity onCreate() method add this to finish the MainActivity

if( getIntent().getBooleanExtra("Exit me", false)){
    finish();
}

结果和上面一样,但是因为你所有堆积的活动都关闭了,当你回到你的应用程序时,它必须从你的主要活动开始,即启动器活动.

The result will be same as above, but because all your stacked up activities are closed, when you come back to you app it must start from your main activity i.e launcher activity.

希望这会有所帮助.

这篇关于System.exit(0) 没有关闭我所有的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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