Android 活动生命周期 - 根据安装方法的不同而不同?
我刚刚注意到我的 Android 2.3.3 应用程序有一些奇怪的行为。我可以编写一些额外的代码来处理它,但我想首先了解为什么会发生这种情况!
当我通过 Xcode 安装时(只需按 F11 并正常调试),我的活动之一的生命周期如下:当我简单启动应用程序时,让活动出现,按设备上的主页按钮将其关闭(最小化它) ),然后再次打开它。
onCreate
onStart
onResume
onPause
onStop
onRestart
onStart
onResume
但是,如果我将应用程序导出到 APK 并通过电子邮件安装它,我会得到以下行为:
onCreate
onStart
onResume
onPause
onStop
onCreate ******
onStart
onResume
...这完全相同,除了这次当我重新打开应用程序时调用 onCreate 。
我查看了生命周期文档,我认为在恢复时必须先调用 onDestroy,然后才能调用 onCreate?这是一个错误的假设吗?
谢谢,
史蒂文
I just noticed some weird behaviour with my Android 2.3.3 application. I can write some extra code to deal with it, but I'd like to understand why it's happening in the first place!
When I install via Xcode (just by hitting F11 and debugging as normal) the lifecycle for one of my activities is as follows when I simply start the app, let the activity appear, press the home button on my device to close it (minimize it), then open it up again.
onCreate
onStart
onResume
onPause
onStop
onRestart
onStart
onResume
However, if I export the application to an APK and install it by email, I get this behaviour:
onCreate
onStart
onResume
onPause
onStop
onCreate ******
onStart
onResume
... which is exactly the same, except that onCreate is called this time when I re-open the app.
I've looked at the lifecycle documentation and I thought that onDestroy had to be called before onCreate could be called when resuming? Is that a wrong assumption?
Thanks,
Steven
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了回答这个问题,我必须提出几个假设。
1) 您正在通过放置在活动事件中的日志来跟踪生命周期
2)系统没有任何其他改变
3) 调试器帮助活动在停止时保持活动状态
您可能没有指示是否调用 onDestroy()。 onCreate() 仅在创建活动时调用(相对于从停止状态恢复)。
正如假设中所述,所使用的调试器可能会强制系统在停止状态下保持应用程序处于活动状态。当您从 APK 加载它时,它并未处于调试状态,因此没有任何东西可以强制它保持活动状态。一旦调用 onStop(),系统可以调用 onDestroy() 快速终止应用程序以释放内存。发生这种情况后,必须再次调用 onCreate() (因为它被破坏了)。
您可能已经读过这篇文章,但您可以看一下:
http://developer.android.com/reference/android/app/Activity.html
To answer this I am going to have to assert several assumptions.
1) You are tracking the life cycles by Log placed in the events for your activity
2) Nothing else was changed about the system
3) They debugger assists in keeping the activity alive when stopped
You may not have an indicator for whether onDestroy() is called. onCreate() is only called when the activity is created (as against to resumed from a stopped state).
As stated in the assumptions, the Debugger used is probably forcing the system to keep the app alive while in a stopped state. When you load it from the APK it is not in debug and so nothing is forcing it to stay alive. Once the onStop() is called, the system can kill the app to free up memory very quickly, calling onDestroy(). After this happens, onCreate() would have to be called again (because its was destroyed).
You probably already read this but here you go:
http://developer.android.com/reference/android/app/Activity.html
从我的角度来看,Pyrodante 的答案并不是 100% 正确(我不能写注释,所以我必须写一个答案):
OnDestroy()
在调试器和apk-variants,如果你的两个列表都正确的话。这是没有意义的:只有当 Activity 被销毁(达到其生命周期)时才会调用 onDestroy() 。因此,同一个活动对象永远不会在onDestroy()
之后调用onCreate()
。如果进程被终止以释放内存:活动必须暂停 (
onPause()
) 或停止 (onStop()
),系统会终止进程以释放内存内存,如果再次需要该 Activity,则会调用onCreate()
。您的 APK 变体就是这种情况。请参见下图。有时这对我很有帮助,拿一支铅笔并在其中画箭头,就像我的应用程序运行的方式一样:
The answer from Pyrodante is not 100% correct from my point of view (I can't write comments, so I have to write an answer):
OnDestroy()
isn't called in both debugger- and apk-variants, if both of your lists are right. It wouldn't make sense:onDestroy()
is only called, when the activity is destroyed (reached it's end of life). SoonCreate()
will never be called afteronDestroy()
by the same activity object.In the case the process is killed to free memory: the activity must be paused (
onPause()
) or stopped (onStop()
), the system kills the process to free memory, and if the activity is needed againonCreate()
is called. This is the case at your APK-variant.See the following image. Sometimes it helps for me, to take a pencil and draw arrows in into it, the way my app runs: