继续学习Android,我读了以下内容:

Question: Does the user have a choice to kill the application unless we put a menu option in to kill it? If no such option exists, how does the user terminate the application? Answer: (Romain Guy): The user doesn't, the system handles this automatically. That's what the activity lifecycle (especially onPause/onStop/onDestroy) is for. No matter what you do, do not put a "quit" or "exit" application button. It is useless with Android's application model. This is also contrary to how core applications work.

呵呵,我在Android世界里每走一步都会遇到一些问题=(

显然,你不能在Android中退出应用程序(但Android系统可以随时完全销毁你的应用程序)。这是怎么回事?我开始认为这是不可能写一个应用程序的功能作为“正常的应用程序”-用户可以退出应用程序时,他/她决定这么做。这不应该依靠操作系统来完成。

我正在尝试创建的应用程序不是Android市场的应用程序。它不是一个被大众“广泛使用”的应用程序,它是一个将在非常狭窄的商业领域使用的商业应用程序。

我其实很期待为Android平台开发,因为它解决了Windows Mobile和。net中存在的许多问题。然而,上周对我来说有点令人失望……我希望我不必放弃Android,但它现在看起来不太好=(

有办法让我真的退出应用程序吗?


当前回答

这是一个有这么多专家参与的有趣而有见地的讨论。我觉得这篇文章应该从Android开发的主要网站中循环回来,因为它确实围绕着Android操作系统的核心设计之一。

我还想在这里发表我的意见。

到目前为止,我对Android处理生命周期事件的方式印象深刻,它将类似web的体验概念引入原生应用。

话虽如此,我仍然认为应该有一个退出按钮。为什么?... 不是为了我,不是为了泰德,也不是为了在座的任何一位技术大师,而是为了满足终端用户的需求。

虽然我不是Windows的忠实粉丝,但很久以前他们引入了一个大多数终端用户都习惯的概念(X按钮)……“我想在‘我’想要的时候停止运行一个小部件”。

这并不意味着有人(操作系统、开发人员?)会根据自己的判断来处理……它仅仅意味着“我习惯的红色X按钮在哪里”。我的行为应该类似于“按下按钮结束通话”,“按下按钮关闭设备”,等等……这是一种感知。它本身带来了一种满足感,我的行动确实达到了目的。

尽管开发人员可以使用这里给出的建议来欺骗这种行为,但仍然存在这样的看法,即应用程序应该完全停止运行(现在),由最终用户要求的独立、可信和中立的来源(OS)。

其他回答

不要把您的应用程序看作一个整体应用程序。它是一组UI屏幕,用户可以通过Android服务与你的“应用程序”和“功能”进行交互。

不知道你的神秘应用程序“做什么”并不重要。让我们假设它通过隧道进入某个超级安全的企业内部网,执行一些监控或交互,并保持登录状态,直到用户“退出应用程序”。因为您的IT部门命令它,所以用户必须非常清楚他们何时进入或退出内部网。因此你认为用户“退出”很重要。

这很简单。创建一个服务,在通知栏中放置一个持续通知,说“我在内部网中,或者我正在运行”。让该服务执行应用程序所需的所有功能。拥有绑定到该服务的活动,以允许用户访问与“应用程序”交互所需的UI部分。并有一个Android菜单->退出(或登出,或其他)按钮,告诉服务退出,然后关闭活动本身。

This is, for all intents and purposes exactly what you say you want. Done the Android way. Look at Google Talk or Google Maps Navigation for examples of this "exit" is possible mentality. The only difference is that pressing back button out of your activity might leave your UNIX process lying in wait just in case the user wants to revive your application. This is really no different than a modern operating system that caches recently accessed files in memory. After you quit your windows program, most likely resources that it needed are still in memory, waiting to be replaced by other resources as they are loaded now that they are no longer needed. Android is the same thing.

我真不明白你有什么问题。

Android上下文中的应用程序只是一堆模糊相关的活动,退出应用程序并没有多大意义。你可以finish()一个Activity, Activity栈中前一个Activity的视图就会被绘制出来。

在Intent中使用FLAG_ACTIVITY_CLEAR_TOP标志关闭应用程序,然后system.exit();

或者有类似的方法,但是没有system.exit()当你想退出时调用这个方法:

public void exit() {
    startActivity(new Intent(this, HomeActivity.class).
    setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK).putExtra(EXIT_FLAG, true));
}

在你的HomeActivity.onCreate()中添加以下代码

protected void onCreate(Bundle savedInstanceState) {
    if (getIntent().getBooleanExtra(EXIT_FLAG, false)) {
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) == 0) {
            finish();
        }
    }
......................

这将在不破坏Android生命周期的情况下工作。

回答:(罗曼盖伊):用户不需要,系统会处理这个问题 自动。这就是活动生命周期(特别是 onPause/onStop/onDestroy)用于。无论你做什么,都不要放不下 “退出”或“退出”应用程序按钮。这在安卓系统中毫无用处 应用程序模型。这也与核心应用程序的方式相反 工作。

1: Totally exiting an application may be generally unmandatory, but it is not useless. What if windows had no exit option? System would be doggy slow as memory was full and the OS had to guess at which programs you were done with. I don't care what Romain Guy or even Larry Page and Sergey Brin say - these are unquestionable facts: Systems run slower when they have to kill tasks to get their memory before a new app can be launched. You just can't tell me that it doesn't take time to kill an app! Even the light from distant stars take time... There is some use in allowing the user to fully close apps.

2:与核心应用程序的工作方式相反?这是什么意思?当我现在运行一个应用程序时,它不再做任何工作……它只是在需要内存时等待被操作系统杀死。

总而言之,最小化和退出之间有明显的区别,两者都不适合对方。每个螺丝上都留个螺丝刀吗?还是每扇门都有钥匙?我们是不是把所有的电器都开着,直到断路器爆炸,我们需要打开另一个电器?我们是不是把洗碗机里装满了盘子,每次只拿出足够的空间来放一些新的脏盘子?我们把所有的车都停在车道上,直到——哦,算了。

如果用户想要最小化应用程序,那么最好的方法就是最小化它。如果用户想要退出应用程序,那么无论如何最好是退出。

这会让人皱眉吗?这是Android的观点——他们不赞成。许多独立的Android新手开发者对此表示不满。

但当它落到实处时,有好的编码和坏的编码。有好的程序流模型也有坏的程序流模型。

当用户知道他们已经用完了程序时,把程序留在内存中并不是好的程序流程。它完全没有任何用处,而且在启动新应用程序或运行应用程序分配更多内存时,它会减慢运行速度。

这有点像你的汽车:有时你让它一直开着,比如在红绿灯前停车,或者在快餐店经过时停车,或者在自动取款机前停车。但在其他情况下,你确实想把它关掉——比如当你去工作的时候,或者在杂货店,甚至在家里。

同样地,如果你正在玩游戏,这时手机响了。暂停游戏并继续运行。但如果用户已经玩了一段时间,那就让他们退出游戏吧。

The exit button on some applications should be more out in front than others. Games, for example, or programs where the user is likely to want to fully exit, should have an obvious exit. Other programs, like, perhaps, email programs, where exiting is an unlikely desire (so that it can keep checking for email) -- these programs should not waste prime control input screen space with an exit option, but for good program flow, it should have an exit option. What if someone decides they don't want their mail program trying to check email when they are in poor coverage area, or maybe in a Skype call or whatever? Let them exit the email program if they want!

暂停和退出是两项至关重要的任务,两者都不能实现对方的作用。

我同意泰德的观点。我明白退出应用程序不是 “Android方式”,但它似乎不应该被排除。在这里 您可能需要一个真正的应用程序出口(不是 只是活动):

The user might want some control over which app gets killed in the case of low memory. If important app A is running in the background, then you might like to exit app B when you are done with it so that app A doesn't get killed by the operating system. If your application has sensitive data cached in memory, you might like to kill the app so that a virus/worm/rogue app can't get at it. I know the security model is supposed to prevent that, but just in case... If your application uses resources (like network, CPU, sensors, etc.) that could adversely affect the phone, then one way of ensuring that those resources are freed up is to exit the application. I understand that well-behaved apps should free up resources when they are not needed. But again, exiting the application seems like a reasonable way of ensuring that.