6

我的应用程序中有活动(A,B,C)。当我启动应用程序活动 A 时:

  A:onCreate()
  A:onStart()
  A:onResume()

使用意图我正在调用第二个活动(A-> B):

  A:onPause()
  B:onCreate()
  B:onStart()
  B:onResume()
  A:onStop()

然后我点击“主页”按钮所以应用程序进入后台:现在

  B:onPause()
  B:onStop()

1 或 2 小时后,我再次转到设备中的主页,然后单击它运行的应用程序图标,如下所示:

 B:onDestroy()
 A:onRestart()
 A:onStart()
 A:onResume()

但我需要像这样退出哪一项活动,

B:onRestart()
B:onStart()
B:onResume()

我读过一些文章,它说该活动因不再可见而被系统杀死。有没有可能解决我的问题...

提前致谢...

4

2 回答 2

10

You may be confusing two different things here:

  1. Android does not kill an activity if it needs memory. What it does is that it kills the whole process that the activity is running in. In general that means that Android kills all of your activities in this situation. However, it remembers the activity stack and when the user returns to the application, Android will create a new process and then recreate each activity (in turn, as needed). It starts by recreating the activity that was on the top of the activity stack (ie: where the user left the application).

  2. Android assumes that if the user leaves a task for a long period of time (I think this is something like 30 minutes) then he is no longer interested in that task and there is no point in remembering where the user was in the activity stack of that task because he probably doesn't care anymore. In this case, what happens is that when the user returns to the task (or restarts the application that was on the top of the activity stack in that task) Android simply clears the task back to the root activity. This has the effect that it looks like the application is starting all over again. This is the desired (and documented behaviour).

What you want to do is prevent Android from clearing the task in situarion #2. You do it by adding

    android:alwaysRetainTaskState="true"

to the <activity> tag of the root activity (ie: the activity that starts your application, the one with ACTION_MAIN and CATEGORY_LAUNCHER).

于 2012-07-26T16:54:35.227 回答
1

我不相信这是你可以控制的事情。如果您的 Activity 长时间处于后台,同时其他应用程序需要内存,系统将终止您的 Activity 以释放内存。

于 2012-07-26T08:43:23.383 回答