Android Activity Life Cycle
Have you ever thought what is proceeding in the background while an activity is being constructed?
An activity represents a single screen. Android system initiates its program with in an activity by calling the onCreate() method. There is a sequence of callback methods that initiates an activity and a sequence of callback methods that terminate an activity. This known as “Activity Life Cycle”.
The following diagram shows the important state paths of an activity.
Let’s explain the life cycle methods of the activity and how the activity will behave in different states.
The OnCreate() method is called when the activity is created. In this method, operations that are used only once for the entire life of the activity, such as binding data to lists, creating objects, are happen.
The onStart() method makes the activity visible to the user, while the app prepares for the activity to enter the foreground and become interactive.
After this step, the user can interacts with the app. The application stays in this mode until there is any interruption such as receiving a phone call.
For some reason, our current activity may need to go into the background, such as when the alarm rings or we may want to go to another activity. In such cases, our current activity is thrown to the background and the onPause() method is called. After the onPause() method is examined, our activity starts to wait in the background and when we return to the activity, it starts running from onResume() again.
The waiting time in onPause() is uncertain and the activity waiting for a long time in the background are switched to stop mode with the onStop() method. In stop mode if the activity is terminated due to some reason such as memory requirement, onCreate() method is called again.
The onRestart() method is called when we return to the activity while in stop mode. The onStart() method is called again after the OnRestart() method.
When the activity is terminated by the operating system or the user, the onDestroy() method is called and all objects of the activity and itself are dropped from memory.
Thank you for reading.