简体   繁体   English

按返回键无法完成我的应用。 但是将其从最近的应用列表中删除可以

[英]Press back key can't finish my app. But removing it from recent apps list can

I am creating an Activity with android:launchMode="singleTask". 我正在使用android:launchMode =“ singleTask”创建一个Activity。 In the app, there is another class, not declared in the activity, which keeps itself a static final instance and some journal to show in the activity. 在应用程序中,还有一个未在活动中声明的类,该类使自己保持静态的最终实例,并在活动中显示一些日志。 When I try to finish the activity by press the back key. 当我尝试按返回键完成活动时。 The log show the activity is destroyed, as I put 日志显示活动被破坏,正如我所说

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("I am destroyed", ".");
}

code in the activity. 活动中的代码。 However, when I start the app again, I find the activity is still the same as I leave it. 但是,当我再次启动该应用程序时,发现活动仍然与我离开它时相同。 Then, I press the back key, and remove it from the recent apps list manually. 然后,我按返回键,然后将其从最近的应用程序列表中手动删除。 And run the app again. 并再次运行该应用程序。 This time, the app shows as it should be. 这次,该应用显示为应有的状态。

What's the different between press the back key to finish a app and remove it in recent apps list? 按返回键完成应用程序并将其从最近的应用程序列表中删除有什么区别? And what should I do if I want to finish the app as normal apps do? 如果我想像普通应用一样完成应用程序,该怎么办?

Each android application gets it's own process , and in that process, one more more activities can start and finish many times without the process ending. 每个android应用程序都有其自己的进程 ,在该进程中,可以再进行多次活动,而无需结束该进程。

Static variables don't go out of scope when an Activity ends, but only when the process finally ends. 当一个Activity结束时,静态变量不会超出范围,而只会在该过程最终结束时才超出范围。

You could end the entire process by calling System.exit(0) however that sort of thing is usually discouraged and a better solution would be to reinitialize the relevant values when the activity is destroyed. 您可以通过调用System.exit(0)结束整个过程,但是通常不鼓励这种事情,并且更好的解决方案是在销毁活动时重新初始化相关值。

edit: See this post for more details about closing an Android application: 编辑:有关关闭Android应用程序的更多详细信息,请参见这篇文章:

How to close Android application? 如何关闭Android应用程序?

"Back"ing out of your app does not mean it is finished. 向应用程序“返回”并不意味着它已完成。 Android decides when it's finished, if at all. Android会决定何时结束(如果有的话)。 This is by design, is intended to provide fast launching of apps and works very well. 这是设计使然,旨在提供应用程序的快速启动,并且运行良好。 You haven't shown any code so it's only a guess but you are probably doing too much in onCreate() and ignoring onResume(). 您没有显示任何代码,所以这只是一个猜测,但是您可能在onCreate()中做太多了,而忽略了onResume()。 Take a look at the Android activity lifecycle: 看一下Android活动生命周期:

http://developer.android.com/reference/android/app/Activity.html#Lifecycle http://developer.android.com/reference/android/app/Activity.html#Lifecycle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM