背面按钮的Andr​​oid关闭App [英] Android close app on back button

查看:111
本文介绍了背面按钮的Andr​​oid关闭App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序看起来是这样的: MainActivity - > UserActivity - > DetailsActivity。 (活动大致顺序)。

The application looks something like this: MainActivity -> UserActivity -> DetailsActivity. (approximate order of activities).

我想关闭该应用程序被点击DetailsActivity时,后退按钮(第三个活动的行中)。

I would like to close the application when the back button is clicked in DetailsActivity (third activity in the row).

想知道,如果它是很好的做法,以做到这一点,什么是做到这一点的最好方法是什么?

Wanted to know if it's good practice to do that and what would be the best way to do that?

推荐答案

如果我理解正确的话,你要关闭的活动,即使栈不为空,这意味着在堆栈超过1活动?

If I understand you correctly, you want to close activity even when the stack isn't empty, meaning there is more than 1 activity in stack?

那么,如果只有一个...只是:

Well if there is only one... just :

finish();

否则,诀窍是:

Otherwise the trick is :

Intent intent = new Intent(Main.this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

和在同一个活动的onCreate

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

所以,你清除栈,然后杀死一个一个离开...你可以在任何活动做到这一点,当然在 onBack pressed 使用它: )

这篇关于背面按钮的Andr​​oid关闭App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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