Android Activity and Service Life-cycle notes

Hello,

In the past, there have been many times that Activity or Service lifecycle confuse me, and I usually have to go back to the corresponding Android SDK documentation pages, which are very analytic and helpful (of course!). In order to clarify what I need to implement, sometimes I have to read the whole document, until I find what I need, making me read stuff that I have already passed through in the past. So, I decided to keep some notes, which I will present here for future reference, and who knows, these might help you! Some parts are written in my own words, while others have been copied from the Android SDK documentation.

Activity Life-cycle

onCreate: This is where the UI is defined. This method is called only the first time that the activity instance is launched and when it has been dropped by the system

onPause: User is leaving the activity, so any changes made, should be committed

onStart: Class members are still alive, as the class was just stopped, so the memory costy resources that have been released, should be re-initialized at this state.

onDestory: Activity is finishing or being destroyed by the system.

Entrire Lifetime:
– Between the onCreate and onDestroy events
Visible Lifetime
– Between the onStart and onStop events
Foreground Lifetime
– Between the onResume and on Pause events. An application might pass frequently from these stages, so no heavey load should be performed during these transitions.

Status transitions:
1. Activity at the top of the stack is active or running
2. Activity lost focus but is still visible, when is paused. Can be killed by the system in extreme low memory situations.
(it maintains all state and member information)
3. Activity has been completely obscured by another activity, then its stopped. It is no longer visible to the user so its
window is hidden and it will often be killed by the system when memory is needed elsewhere.
(it maintains all state and member information)
4. While an activity being in paused or stopped state, it might be dropped by the system, in order to release memory. When viewed again, it has to be completely restarted from its previous state.

Coordinating activities:
– Activity A’s onPause() method executes.
– Activity B’s onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
– Then, if Activity A is no longer visible on screen, its onStop() method executes.

Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks. This method  is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, which will be passed to the new object through the onCreate(Bundle) method, if the activity needs to be re-created.

Service Life-cycle

onCreate: service is instantiated and setup

onStartCommand(Intent, int, int): service receives a call via startService.

onDestroy: when an activity calls stopService, or the service itself calls stopSelf, or the system decides to destroy the service.

* Using binding, an Activity can connect to a Service and make direct  calls back to the service. This is another method of using services, which cannot be covered in this post. This method of starting and accessing a Service affects the life cycle of the Service.

Two modes of operation depending on the value returned by the onStartCommand method:
– constant START_STICKY is used for services that are explicitly started and stopped as needed.
– constants START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them.

That’s all for now. I have also attached the life-cycle describing diagrams from Android SDK documentation.

Happy android coding!

Advertisements

About sermojohn

Professional Software Engineer
This entry was posted in Android and tagged , , , , , , , , . Bookmark the permalink.

4 Responses to Android Activity and Service Life-cycle notes

  1. Lakshman says:

    For a service, is onDestroy() guaranteed to be called? The Activity life cycle here http://developer.android.com/reference/android/app/Activity.html#09484598008602985151 says that onStop() is “killable” which means Android can decide to kill the process after returning from onStop. Doesn’t it mean that onDestroy may not be called?

    • sermojohn says:

      Hello! Thanks for commenting!
      For a service the onDestroy() should be always called when the service finishes, as its the only point when you can finalize any resources. But for an activity, this might not be the case in very extreme cases, when the system needs to free memory (http://developer.android.com/training/basics/activity-lifecycle/stopping.html), and onDestroy() might never be called after the onStop(). So, to avoid any memory leaks you should better unregister any references when onStop() is called. However, I still onDestroy() for cleaning up resource that can not cause any memory leaks, or else onDestroy() would be useless to use! Hope this helps!

  2. Maidul Islam says:

    Hello ,
    Suppose I am in any stage of Activity or service.Now I press Home Button on my device.Then what is happened? which methods will call?Please give me explanation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s