开发者

Android activity lifecycle - different depending on method of install?

开发者 https://www.devze.com 2023-03-25 11:55 出处:网络
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 un开发者_StackOverflow中文版derstand why it\'s happening in the fir

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 un开发者_StackOverflow中文版derstand 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


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


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). So onCreate() will never be called after onDestroy() 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 again onCreate() 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:

Android activity lifecycle - different depending on method of install?

0

精彩评论

暂无评论...
验证码 换一张
取 消