The Activity Lifecycle in Android
Activity
It is one of the four main components of android and is generally the App’s Entry Point.
Unlike other programming paradigms where most of the programs are started using a main() method, Android launches code in an Activity instance through specific callback methods during various stages of its lifecycle.
The Activity instances in an Android app go through various states in their lifecycle as the user navigates through, exits, and re-enters the app. The Activity class offers several callback methods that inform the activity of state changes, such as creation, stopping, resuming, and destruction of the activity process.
Within the lifecycle callback methods, you can declare how your activity behaves when the user leaves and re-enters the activity. The best example is a video player application. There can be multiple states when you are playing a video, like Play, Start, Pause, Resume, Stop, etc. The same thing exists for activities, and we have a specific method for every single state of our activity, called the Lifecycle Callback method.
To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.
The above image is the easiest illustration of the activity lifecycle.
As you can also see, when we open our application or any activity, one activity has to go through those stages/methods of onCreate(), onStart()and onResume(). So, that means we can add all our initializations inside these methods, and we do the same thing, that is to attach our layout in onCreate() callback.
Corresponding to this, we will call those specific methods when we leave our app/acitivity. So, now let me tell you which method to call in which case.
onCreate()
This fires when the system first creates the activity. On activity creation, the activity enters the Created state. In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity. For example, binding layout or your ViewModel.
onStart()
When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. Use this where the app initializes the code that maintains the UI, like starting animation or attaching listeners or receivers.
onResume()
When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume() callback. This is the state in which the app interacts with the user. The app stays in this state until something happens to take focus away from the app. Such an event might be, for instance, receiving a phone call, the user navigating to another activity, or the device screen turning off.
onPause()
The onPause() callback method is called when an activity is no longer in the foreground, either because another activity has taken focus or because the user has navigated away from the app. In this method, you should perform any necessary actions that should happen before the activity becomes partially or completely obscured, like stopping animation or persisting UI states.
onStop()
This method is called when an activity is no longer visible to the user and is in the process of being stopped. In this method, you should perform any necessary actions that should be done when the activity is no longer visible. Like, releasing the resources, and unregistering listeners or receivers.
onPause() is called when an activity is losing focus, while onStop() is called when the activity is no longer visible to the user.
onDestroy()
onDestroy() is called before the activity is destroyed. The system invokes this callback either because:
the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode).
Instead of putting logic in your Activity to determine why it is being destroyed, you should use a ViewModel object to contain the relevant view data for your Activity.
In your android studio Press ctrl + o to search add your required method. Adding a callback method in your activity class will look like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)// Calling parent class Method
setContentView(R.layout.activity_main)// Binding our layout
// Write your code here…
}
In this image, you can see there is a corresponding destructive method for every constructive one. So, use this concept to create error-free applications, whenever you are registering some listener, or receiver or starting any animation or doing any other thing in a constructive method, don’t forget to unregister/dismantle the thing in the corresponding destructive method.
Source: https://developer.android.com/
MUST CHECK OUT OFFICIAL DOCS, THEY ARE OUTSTANDING TOO❤.
Follow if you understand this concept and want to learn more Android-related topics.
#xDaysOfAndroid #AndroidWithSagar #android #androiddevelopment