继续学习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,但它现在看起来不太好=(
有办法让我真的退出应用程序吗?
目前,我在我的应用程序中实现了以下内容。可能这些有助于从应用程序中移动出来,无论你想要的是什么。我从操作栏菜单中调用这个函数。
public static void exitApplication(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
exitApplicationHC(context);
}
else {
exitApplicationPreHC(context);
}
}
private static void exitApplicationPreHC(Context context) {
Intent i = new Intent(context, LoginActivity.class);
i.putExtra(EXTRA_EXIT, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
if (context instanceof Activity) {
((Activity) context).finish();
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static void exitApplicationHC(Context context) {
Intent i = new Intent(context, LoginActivity.class);
i.putExtra(EXTRA_EXIT, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);
}
我同意泰德的观点。我明白退出应用程序不是
“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.