继续学习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,但它现在看起来不太好=(

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


当前回答

有退出按钮的一个重要原因是“退出”广告。在出口,可以显示一些产生收入的广告。像所有的广告一样,它还是有点烦人,但也许比那些占用宝贵屏幕空间的广告更烦人。一些广告网络提供这种广告方式。但是,真的,你不能只是放一个退出按钮,在显示广告后什么都不做!

因此,在某些情况下需要一种或另一种方式来终止程序,而“永远不应该被需要”可能不是最全面的答案。

可能会使用Activity.finish()或System.exit(0)。

其他回答

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

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

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

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

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

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

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

All of my applications have quit buttons... and I quite frequently get positive comments from users because of it. I don't care if the platform was designed in a fashion that applications shouldn't need them. Saying "don't put them there" is kind of ridiculous. If the user wants to quit... I provide them the access to do exactly that. I don't think it reduces how Android operates at all and seems like a good practice. I understand the life cycle... and my observation has been that Android doesn't do a good job at handling it.... and that is a basic fact.

我会考虑阅读Addison-Wesley出版的“Android Wireless Application Development”。我刚刚完成它,它是非常彻底的。

看来你对Android平台有一些基本的误解。一开始我也对Android应用程序的生命周期感到有点沮丧,但在更深入地了解后,我开始真正享受这种方法。这本书将回答你所有的问题,甚至更多。这确实是我为Android新开发者找到的最好的资源。

Also, I think you need to let go of a line-for-line port of the existing app. In order to port your application to the Android platform, some of the application design is going to change. The application-lifecycle used is necessary as mobile devices have very limited resources relative to desktop systems and allows Android devices to run several applications in an orderly and resource-aware fashion. Do some more in depth study of the platform, and I think you will realize that what you are wanting to do is entirely feasible. Best of luck.

顺便说一下,我与艾迪生-卫斯理或与这本书有关的任何个人或组织都没有任何关系。重读了我的帖子后,我觉得我有点像个迷弟。我真的非常非常喜欢它,而且发现它非常有帮助。:)

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

如果你指定API >= 16, Activity#finishAffinity()可以满足你的需求。