Activity Lifecycle in Android
Activity Lifecycle helps to understand the behavior of activity when the user enters and leaves the application. Activity instance of the app transition through different states of the life cycle. There are number of life cycle callback methods been called which briefs the understanding on the different states and if the states are changed such as creating, stopping, resuming, destroying the process in which the activity resides.
The methods called in activity life cycle are as follows :
- oncreate()
- onstart()
- onresume()
- onpause()
- onstop()
- onrestart()
- ondestroy()
Activity life cycle surrounds between 4 states that are as follows :
- Doesn’t exist : This is a state where android app is not yet opened.
- Foreground : This is a state where android app is opened i.e the app screen is in foreground.
- Background : This is a state where another screen of the app is opened and screen in foreground goes to background.
- Pause : This is a state where while the app is in foreground and you get a phone call or alert dialog over it and the opened screen is paused.
Now lets understand which of the methods are called while travelling from one state to another state.
- Doesn’t exist – Foreground : When we open a app and a screen gets into foreground state then following are the methods called :
- oncreate()
- onstart()
- onresume()
- Foreground – Background : When we open another screen , then the screen in foreground goes to background state and the methods then called are :
- onpause()
- onstop()
- Foreground – Pause : When we get a call while operating on the current screen, then the current screen is paused and the method called is :
- onpause()
- Pause – Foreground : When the phone call or alert dialog is dismissed and the foreground is again resumed from onpause state the the method called is :
- onpause()
- Background – Foreground : When you finish the current activity and the activity which went background comes back to focus i.e foreground then the methods called are :
- onresume()
- onstart()
- onrestart()
- Foreground – Doesn’t exist : When all the activities in the stack is finished and the foreground activity enters into doesn’t exist state then the following methods called are :
- ondestroy()
- onstop()
- onpause()
Please do share your thoughts and any queries on this post in the comments below and please do like my post by giving +1 and by sharing it.
0 Comments