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

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


当前回答

回答:(罗曼盖伊):用户不需要,系统会处理这个问题 自动。这就是活动生命周期(特别是 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!

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

其他回答

我会考虑阅读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的视图就会被绘制出来。

对于应用程序的第一个(启动)活动,

@Override
public void onBackPressed(){

    // Exit
    moveTaskToBack(true);
}

为我工作。我想在这里关闭应用程序。从其他活动中回来;我用了意图,例如:

@Override
public void onBackPressed(){

    // Going back....
    Intent intent = new Intent(ActivityB.this, ActivityA.class);
    startActivity(intent);
    finish();
}

注意:这段代码适用于开发人员想要从ActivityZ返回ActivityA然后关闭应用程序的场景。

目前,我在我的应用程序中实现了以下内容。可能这些有助于从应用程序中移动出来,无论你想要的是什么。我从操作栏菜单中调用这个函数。

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开发新手,我开始熟悉生命周期等等。作为一名Android用户,我一直讨厌自己无法删除应用程序。

为什么用户应该信任一个应用程序?我们可能认为把应用放在后台是“安全的”,但用户真的安全吗? 我们可能会爱上“新”做事方式的天才,但并不是所有的应用程序都写得很完美,甚至很好。有些可能是恶意的,并试图保持后台进程一直运行。有些可能是出于好意,但很混乱。

我讨厌打开浏览器或谷歌,从我上次离开的地方开始,不得不向后堆叠几十个缓慢的页面,只是为了感觉我有一个干净的开始。用户应该拥有最终的控制权。有多少次技术支持告诉我们“重启我们的机器”或“关闭程序并重新启动”?用户需要感觉他们是在重新启动应用,而不是恢复一个可能会让他们沮丧或给他们带来问题的状态。

你不能指望人们保留一个复杂的环境模型,只是为了使用一个应用程序来完成一些事情。人们觉得自己可以控制铅笔和纸,因为这体现在他们对铅笔和纸的行为和未来行为的体验中。 软件是魔术,它发生在幕后。它的行为规则就像创建它的开发者一样反复无常。

We should try to design appliances that relate to an underlying, almost physical, model that is robust and reliable and truly intuitive to the user. "Killing" an app is something a user can embrace. It's like throwing out a pile of scratch paper and starting over; closing a book and putting it back on the shelf. Magic has its place for dedicated professionals who can invest themselves in a particular world, such as video editing or animation systems. And these users often contribute to the features themselves and so are comfortable with them. But everyday users deserve at least a few really grounded options they can rely on regardless of sophistication level, in my opinion. I'm for an easy way to exit a process completely even if it is not the target model the system aspires to.