简体   繁体   English

Android活动生命周期-取决于安装方法吗?

[英]Android activity lifecycle - different depending on method of install?

I just noticed some weird behaviour with my Android 2.3.3 application. 我刚刚注意到我的Android 2.3.3应用程序有些奇怪的行为。 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. 当我通过Xcode安装时(只需按F11并按常规进行调试),当我仅启动应用程序时,我的活动之一的生命周期如下所示,让该活动出现,请按设备上的主屏幕按钮将其关闭(将其最小化) ),然后再次将其打开。

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: 但是,如果我将应用程序导出到APK并通过电子邮件安装,则会出现以下现象:

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. ...完全相同,只是这次当我重新打开应用程序时调用了onCreate。

I've looked at the lifecycle documentation and I thought that onDestroy had to be called before onCreate could be called when resuming? 我查看了生命周期文档,并认为在恢复时必须先调用onDestroy才能调用onCreate吗? Is that a wrong assumption? 那是一个错误的假设吗?

Thanks, 谢谢,

Steven 史蒂文

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 1)您正在通过放置在活动中的事件中的日志来跟踪生命周期2)系统的其他内容都没有更改3)它们的调试器有助于在活动停止时保持活动

You may not have an indicator for whether onDestroy() is called. 您可能没有指示是否调用了onDestroy()的指示器。 onCreate() is only called when the activity is created (as against to resumed from a stopped state). onCreate()仅在创建活动时才调用(与从停止状态恢复相反)。

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. 当您从APK加载它时,它并未处于调试状态,因此没有任何因素迫使它保持活动状态。 Once the onStop() is called, the system can kill the app to free up memory very quickly, calling onDestroy(). 调用onStop()后,系统可以调用onDestroy()终止应用程序以非常快地释放内存。 After this happens, onCreate() would have to be called again (because its was destroyed). 在这种情况发生后,必须再次调用onCreate()(因为它已被销毁)。

You probably already read this but here you go: http://developer.android.com/reference/android/app/Activity.html 您可能已经读过这篇文章,但是您可以在这里找到: http : //developer.android.com/reference/android/app/Activity.html

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): 从我的角度来看,Pyrodante的答案不是100%正确(我无法写评论,所以我必须写一个答案):

OnDestroy() isn't called in both debugger- and apk-variants, if both of your lists are right. 如果两个列表都正确,则在调试器和apk变量中都不会调用OnDestroy() It wouldn't make sense: onDestroy() is only called, when the activity is destroyed (reached it's end of life). 这是没有意义的:仅在活动被销毁(到达生命的尽头onDestroy()时才调用onDestroy() )。 So onCreate() will never be called after onDestroy() by the same activity object. 因此,相同的活动对象永远不会在onDestroy()之后调用onCreate()

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 again onCreate() is called. 如果进程被杀死以释放内存:必须暂停活动( onPause() )或停止( onStop() ),系统将杀死进程以释放内存,如果再次需要该活动,则调用onCreate() This is the case at your APK-variant. APK变体就是这种情况。

See the following image. 参见下图。 Sometimes it helps for me, to take a pencil and draw arrows in into it, the way my app runs: 有时,这对我有帮助,可以用铅笔和箭头插入其中,这是我的应用程序运行的方式: Android生命周期方法

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

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