MainActivity.java
Here is the full explanation of the Android Activity Lifecycle, along with an example:
**onCreate**
: This is the first lifecycle method that is called when an activity is created. This method is used to initialize the activity, set its layout, and perform other one-time setup tasks.Example:
**onStart**
: This method is called after **onCreate**
, when the activity is starting and about to become visible to the user.Example:
**onResume**
: This method is called after **onStart**
, when the activity has become visible and is about to start interacting with the user. This is typically the best place to start animations, music, and other operations that can consume resources.Example:
**onPause**
: This method is called when an activity is about to go into the background, either because a new activity is starting or because the app is about to be closed. This is a good place to save data, stop animations and other operations that should not continue in the background.Example:
**onStop**
: This method is called when an activity is no longer visible to the user. This is a good place to release resources and stop any operations that are not needed in the background.Example:
**onDestroy**
: This method is called when an activity is about to be destroyed. This is a good place to release resources and stop any ongoing operations.Example:
The activity lifecycle methods are called in a specific order, and the order may change based on the actions of the user or system. Understanding the lifecycle and using these methods correctly can help you create a responsive and stable Android app.