What are activity lifecycle methods
What are activity lifecycle methods:
Each activity has different lifecycle methods. These methods are callback methods those got invoked on different state of an activity.
For example, if the user moves from one activity to another activity, different callback methods will be invoked on both activity classes. Again, if the user clicks on the home button, it will move the app to background and another callback method will be invoked.
Video:
You can watch this video to learn more on lifecycle methods with an example:
Importance of lifecycle methods:
You might be wondering why we need to learn about these methods! These methods are really important. These methods are a way to know about different user behaviours. If the user kills our application, or if he move it to background, the only way to know about these events is by using lifecycle methods.
If the app is running a video, we need to pause it once the app moves to the background and resume once it comes back. This can be done by using lifecycle methods.
Activity lifecycle methods:
There are mainly 6 different activity lifecycle methods:
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onDestroy()
Below illustration shows how the activity lifecycle works:
How to get these methods in an Activity:
These methods can be used in an activity as override methods. In any activity class, if you type on, it will show you these methods in Android Studio.
If I implement all of these methods in an activity, it will look as like below:
class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)}override fun onStart() {super.onStart()}override fun onResume() {super.onResume()}override fun onPause() {super.onPause()}override fun onStop() {super.onStop()}override fun onDestroy() {super.onDestroy()}}
If you create a new activity, it will contain onCreate by default.
Explanation of these callback methods:
Let me quickly give you an overview of these callback methods:
onCreate:
Calls when the activity is created. All startup logics are defined inside this method like setting the layout, variable initialization etc. The savedInstanceState is the state of the activity of its previous state. If it is created newly, it will be null.
onStart:
Calls when the activity enters to the started state. The activity is visible to the user at this point.
onResume:
Calls when the activity enters to the resumed state. For example, if the app is moved to background and then move back, it will call this method. This method can be used to resume anything which is paused in the onPause method.
onPause:
This method is called if the activity is moving to background. For example, if we are starting a different activity, app is moved to background, a phone call received etc. This is used mainly to pause any ongoing tasks which can be resumed again in onResume.
In multi-window mode, if we move to a different app, it calls onPause on the application even though the UI is visible.
Also, it's execution is brief. So, it is not a good idea to do time taking operations in this method like save data, network call, DB transactions etc. These tasks might not be completed. These tasks should be done in onStop.
onStop:
It is called when the activity is no longer visible to the user. For example, one new activity is appeared on top of it, the activity is about to terminate etc.
We can use CPU-intensive tasks like saving data to database in this method.
From this state, it the activity moves back to user interaction, it will call onRestart, if it is finished running onDestroy will be called.
onDestroy:
It is called when the activity is destroyed if the user dismissed the activity, finish is called on the activity, destroyed due to configuration change etc.
This method should release all resources.
Please watch the above video to learn more on activity lifecycle methods with one example.