What is Android Activity Life Cycle? Life Cycle Methods and Callbacks – As we learned in the previous section that there are some Events associated with different States of the Lifecycle of an Activity.
Which invoke some Specific Methods and we have to override these methods in the Java Class File of our Activity. In this section, we will try to understand in detail the same methods related to the lifecycle of the activity.
An Android Activity is a Self-Standing complete component of any Android Application, which can be Start, Stop, Pause, Restart or Reclaim depending on the events triggered by the user or system, and in each case A certain method has been allocated to control and handle.
Let us know about these methods in a little detail and try to understand how they help control and handle the lifecycle of our Android App activity.
Void onCreate (Bundle savedInstanceState) method
The lifecycle of any Android App starts with this method. In this method we have to do Associated Layout File Load with our activity. Also, the initialization of all the Variables, Objects, etc. required for our Current Activity is also done in this method, while Declaration of Objects, Variables etc. is normally done at Class Level, so that Other methods of current activity can also be used to access and manipulate each other’s data when needed.
In addition, all the methods we override in our Current Activity, invoke Constructor of their Parent Class as super.parentConstructor () Statement Compulsory as First Statement in all of them. like-
When the onCreate () method is called, then the activity related to it can be in any one of the 3 positions as follows:
It could be the main activity of a brand-new Android App to start, which is going to start its lifecycle.
He may be going to start his lifecycle again due to a configuration change such as an orientation change when the device is rotated, or due to the restart of the app due to some other reason.
It could be the activity of the App restarting after Shutdown due to an error like a low memory.
So when creating an App, it is necessary to keep all these things in mind so that Java Codes related to handling all kinds of situations can be written in the onCreate () method.
Now we can understand the Reference savedInstanceBundle to be created anonymously like Parameter in this method, in which the states of the current activity are saved. That is, we can use this Bundle Instance to know the previous state of current activity.
This Bundle Instance may have been used by the Android System to save States of Current Activity in case there is a change in configuration due to Orientation Change of Android Device, or Current Android App Activity Restart due to Low Memory Has been done In this bundle instance, the previous states that were saved before the activity is restarted, it is known as the activity’s instance state.
Instance State is basically temporary because it stays Tied with the current Android application, so it is not saved as a Permanent File as Secondary Storage because every time that Android App starts, the user will be able to use that Android every time. Reloading the previous state of the app, even if it is not necessary, is not necessary.
However while overriding the onPause () method, we want to keep our Current Android Application States Persisted (as the case may be) for the Long Term. So when override the onPause () method, we can restore the Saved States again via the onCreate () method because as soon as Android App Restart happens, the onCreate () method is executed and saved in theInstanceBundle Reference. Through this, the Current Android App is restored from its previous Instance State.
Here it is important to understand a little more about the situation that when Android App Restart or Re-Create due to change of Orientation of Android Device, then in that case the previous activity is completely Destroyed, so related to it All Instance States are also completely eliminated.
As a result, when the same Android App is restarted, there is nothing to Restore related to the previous States because the new Activity Create from the same Android Application is a Brand New Activity, which has its own new Memory Reference It happens and the states of this new activity do not have any relation with the states of the previous activity.
In this case an External Thread or Global Reference created or maintained by the previous activity is not connected to the Newly Created Activity in any way, as a result they cannot be used in any way in the Current Activity.
Therefore, an arrangement is needed whereby the new activity that has been re-created after the application is restarted can be told what the reference of the previous activity is to the Created or Maintained External Objects.
References of the previous objects created by the previous activity for the newly created activity are called “Non-Configuration Instance Reference” and these External Objects can be referenced by a method named onRetainNonConfigurationInstance ().
The Android SDK then makes these references available to the new activity using a method called getLastNonConfigurationInstance (). Or it can be known that that activity is
Another situation with the onCreate () method is that we have to be certain that when the states of the current activity are saved, then the views and fragments should be the same, which will create the activity when the Android App is restarted.
Be specified in the onCreate () method. Because the onStart () method is executed after the onRestoreInstanceState () method is executed which assumes that the views and fragments to be restored are already present in our Android App’s Hierarchy and if any View from the Hierarchy Or Fragment Missing occurs, they do not recur again.
So any Android App is properly restored again after restarting, this is possible only if the appropriate Layout Views have been loaded with this onCreate () method.
void onStart () Method
The creation of the main activity of an Android App does not mean that it will also be visible in front of the user. In fact the onCreate () method only creates the main activity in memory, the task of making it visible to the user is done by the onStart () method. In other words, this method starts the “Visible Life Cycle” of any activity of any Android App.
This method assumes that the onCreate () method has completely created the View Hierarchy related to the activity and loaded it into memory which is fully ready to be displayed in front of the user.
One feature of this method is that we can also invoke this method manually through the onRestart () method according to our needs.
One important thing to note is that the onRestoreInstanceState () method always executes after the onStart () method is invoke. Therefore, in this method it should never be assumed that the controls of the View Hierarchy related to the activity have been restored from their previous state. Because the work of Restoration takes place after the execution of this method and the View Hierarchy with a fully Restored State is found in the onResume () method.
What is Android Activity Life Cycle? Life Cycle Methods and Callbacks
Therefore, after the Restore from the previous state of Visual Hierarchy, which is Visible, should be written in the onStart () method, without any Operations Codes related to whatever Operations Performs to be performed with them, instead they are onResume () or onRestoreInstanceState () Specify should be done in the method itself.
This method can also be invoke when the activity starts showing again after being hidden due to some other activity. In that case, this method is called after the onRestart () method, which itself is the call after the onStop () method. Therefore, there are two ways to execute this method. First, it is call after onCreate () and second it is call after onRestart () and in both cases it is assumed that the View Hierarchy of the activity is already established.
void onRestoreInstanceState (Bundle savedInstanceState)
If an activity is closed by the user itself, then in that case it is understood that the user does not want to keep the current instance states of that activity saved.
For example, if the user presses the Back Button of the Android Device, it simply means that he is giving the Android Device the Instruction that he is no longer interested in that activity and he has to give all his Unsaved States Close fully while discharging.
So all the states of Current Activity which are not saved yet and are valid only as long as Current Activity is Exist in Memory during its lifecycle, this state of states is known as Instance State.
If the Underlying Android System feels that the Android App should be closed due to the change of orientation of the Android Device by the user, then in that case the Android System should take the Instance State related to the activity of that Android App Comes in when the Android App is restarted and to do so the Android System invokes the onRestoreInstanceState () method and as a Parameter, saves a savedInstanceState Reference Pass of a Bundle Type, which contains Instance State related to Current Activity it happens.
Apart from the instance state, the Persistent State of an activity is the state that the user expects even when the activity is finished. That is, the user wants the various Visual Controls and their contents of an activity in that case, even when they close and reopen that Android App completely, then this state of View Hierarchy is known as Persistent State goes.
Persistent State is a condition that can be created during the current activity lifecycle or it can be exist even before the current activity is created. This type of state is especially required to be manually saved as an External Persistent Store, such as a file, when it is created during the lifecycle of an activity.
If the “Save” button is not available on the activity to save such a state, then in this case this saving of State Saving as Implicit Persistent State should be done in the onPause () method. Because in low-memory Situation there is no guarantee of any other Lifecycle Method being executed after the onPause () method is executed. Therefore, to save any important data, we can never rely on the Instant State, rather we must manually save every important data of this type of Persistent State.
void onResume ()
The execution of this method means that the main activity of our Android App is completely Visible. Also, any Android App activity starts with the same method of Foreground Cycle i.e. Visual Lifecycle and every activity in this Foreground Cycle repeatedly switches between onResume () and onPause () methods as per the requirement.
That is, whenever the Android App Activity with higher Urgency overlaps the Current Activity, the onPause () method is Execute for the current Android App and when the Android App with that higher priority is closed, then Current Android again The onResume () method executes for the activity of the app and this process continues continuously until the current Android App is completely closed.
At the time this method is executed, we can assume that the views and states of their View Hierarchy related to Current Activity have been completely restored and we can use this information for the Final State Changes of our Activity. Huh.
Since there is no bundle type reference in this method, to fine-tune the states, we also have to depend on the information coming from onCreate () or onRestoreInstanceState () methods. Also, if an Animation or Counter is stopped during the onPause () method, it can be restarted again in this method.
void onPause ()
The execution of this callback method means that due to any other high priority activity, the current activity has gone into the background. Therefore, at the time when the activity was completely visible, if a counter or animation run was happening in the activity at that time, then that counter or animation has to stop in this method.
For the activity for which this Method is Execute, any of the onResume () or onStop () method can be Execute. When the onResume () method is executed, the activity in the background is returned to the foreground, while the onStop () method is executed in the Background State when executed. That is, that Android App completely stops.
What is Android Activity Life Cycle? Life Cycle Methods and Callbacks
According to the Android SDK, this is the last method, it is definitely executed when an Android App and its process are completely close. So this method is the last method for any developer, where he can save his Android App’s Persistent and Non-Persistent Data Explicitly as External File.
The Android SDK also waits for this method to be executed before fully activating a foreground activity, so it should try to keep this method as small as possible, so as far as possible, this method should at least write Programming Logic needed. In this method also no reference pass of Bundle Type is done which means that the work related to saving Persistent Data as External Storage (File or Network) should be done with this method. Also we can use this method to stop any kind of Counter or Animation.
That is, all the operations that we have started in our Android App, which need to be closed compulsory, all those operations should be stopped in this method, while if those resumed operations are to be resumed Resume should be done in the onResume () method if needed.
void onStop ()
void onSaveInstanceState (Bundle saveStateBundle)
void onRestart ()
void onRetainNonConfigurationInstance ()
void onDestroy ()