There are 7 life-cycle methods of activity. They are as follows:
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onRestart()
- onDestroy()
In Android, the lifecycle of an activity is managed by a set of callback methods. The key lifecycle methods of an Android activity are as follows:
- onCreate():
- Called when the activity is first created. This is where you perform one-time initialization, such as creating UI elements and setting up data.
- onStart():
- Called when the activity is becoming visible to the user. At this point, the activity is about to become visible, but it’s not yet in the foreground.
- onResume():
- Called when the activity will start interacting with the user. The activity is now in the foreground and actively receiving user input.
- onPause():
- Called when the activity is going into the background. This is typically where you should commit any unsaved changes and pause ongoing processes.
- onStop():
- Called when the activity is no longer visible to the user. This is a good place to release resources that are no longer needed.
- onRestart():
- Called after your activity has been stopped, prior to it being started again. This method is only called if the activity was stopped and is now being restarted.
- onDestroy():
- Called before the activity is destroyed. This is the final callback that the activity receives.
These methods allow you to manage the lifecycle of your activity and handle various transitions between different states. It’s important to properly implement these methods to ensure your app behaves as expected and efficiently manages resources.