Android Activity Lifecycle Methods

Hello readers,

Today’s let focus more on the life cycle methods of an activity and get a grasp on them.

Activity life cycle
onCreate() is called when the activity is created for the first time. It also sets up the initial state of the activity i.e., initialize the activity. This activity is called only once during the entire lifetime of the activity.
onCreate() is typically called to create the following four functions
• Calls super.onCreate() – This will make the android to perform certain initializations
• Sets the content view of the activity such that the user is provided with a visual interface.
• Captures and retains any references to UI views or elements of the user interface.
• Configures the interface views and elements as necessary

Public class MainActivity extends Activity
Protected void onCreate(Bundle savedInstanceState){
/* savedInstanceState is a Bundle containing the activity's previously frozen state .If there is not one, null is passed*/

Here the class Bundle is passed to the onCreate() method . Bundle is used to store the state of the activity. Suppose your activity was destroyed and called again using onCreate() ,in such a scenario the activity which is already present in the Bundle needn’t be created again.
For a newer activity onCreate() call null value is passed

This method is called when the activity is about to be visible.
onStart() can be used to reinitialize variables and reset data
OnStart() is called under two scenarios:
• After onRestart()
• After onCreate()
OnStart() is followed by:
• onResume() /onStop()

This method is called if the activity has been stopped and is to be started again.
OnRestart() is called only after onStop() is called. Generally onRestart() saves the state , refreshes your activity/ reset variables in the activity and after this onStart() is called.

This method is called when the activity is about to start interacting with user. i.e., when the activity is set in the foreground and visible to the user


This method is called when the activity is about to be invisible i.e., when the activity leaves to background and some activity comes into the foreground visible to the user

This method is called when the activity is no longer visible to the user and it may be restarted later .
IF your current activity is slightly visible i.e., if it’s in the foreground but still not very visible only onPause() is called but when the activity is not visible at all then onPause() and onStop() can be called

This method is called when the activity is about to be destroyed. The lifetime of the activity ends with the call of this activity. It is not mandatory that an activity should end with an onDestroy() class.
Either due to low resources or by explicitly press the exit button calling finish() function ,onDestroy() is called.

That’s all folks. Will be posting more such basic blogs on Android. If you guys have any suggestions please do let me know.

Latest posts by abirami vijayakumar (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

займ на карту онлайн круглосуточно