Search code examples
javaandroidlifecycle

Android activity lifecycle - different depending on method of install?


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


Solution

  • 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