In the previous article, I introduced the basic components in an Android application. In this article, I continue the discussion on the life cycle of Android applications.
Android has mechanisms to manage processes according to priority mode. The low-priority process will be freed without warning to ensure resources.
An unusual and basic features of Android is the lifetime of the application process is not controlled directly by itself. Instead, it is determined by the system through a combination of:
- The part of the application that the system is running.
- The important part of how it to users.
- How many regions dominate in system memory.
1. Types of processes in Android:
- Foreground processes: the process of application is currently being interactive.
- Visible process: the process of application activity are displayed to the user (onPaused () of activity is called).
- Service process: the Service is running.
- Background process: the process of application that its activity does not show the user.
- Empty process: the process does not have any component is activated
According to the priority mode when required resources, Android will automatically kill the process, first of all empty process.
2. Life cycle of android application:
Lifecycle component: the application components have a life cycle, that is every element from the beginning of creation and to the end, sometimes they can be active or inactive.
Activity Stack: internal system activity managed as a stack. When a new Activity start, it is placed at the top of the stack and becomes the running activity, the previous activity will pause and below the new activity will not see during the new activity exist.
If the user clicks the Back button, the next activity will moves the stack up and become active.
Activity includes 4 states:
- Active (running): Activity is displayed on the screen (foreground). This activity focuses on the actions of users on application
- Paused: Activity was halted, although it still displayed (visible) but it can not interact (lost focus). It means a new activity above it, but it does not cover the full screen. A pause activity is alive, but it may be terminated by the system in case of lack of memory.
- Stop: Activity replaced completely by a new Activity and will proceed to the stop status. It remains and the information in it. The user does not see it and often discarded when the system needs memory for other tasks.
- Killed: When the system runs out of memory, it will free up the process on the principle of priority. The Activity stop or paused state can be released and when it is displayed again, then this Activity have to restart completely and restored to its previous state.
Activity Diagram state:
Lifecycle of Activity:
- Entire lifetime: From method onCreate () to OnDestroy ()
- Visible liftetime: From method onStart () until OnStop ()
- Foreground lifetime: From method onResume () until onPause ()
When building Actitvity for applications, we need to rewrite the method onCreate () to perform the initialization process. Other methods need to rewrite or not depending on the programming requirements.
The function is executed:
- onCreate (…) This function is called when the Activity class is initialized, the interface used to set up the application and enforcement of basic operations.
- onStart (): This function is called when the application layer appears on the screen.
- onResume () function is called as soon as the user OnStart or application focus, this function will take the application to the top of the screen.
- onPause () function is called when the system is to first focus previous activity.
- OnStop () function is called when a different activity is started and focus.
- onRestart () function is called when the application is switched OnStop (), but want to restart by onStart ().