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.
- 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.
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.
- 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 ofonSaveInstanceState(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.
- 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
No comments:
Post a Comment