Activity Lifecycle in Android
onCreate()
onCreate() is called when the activity is created for the first time. In this method, all the basic work is
done such as creating views and binding data. It can also receive the previous saved state of the
activity if it exists.
onStart()
onStart() is called when the activity becomes visible to the user. It is always called after onCreate().
It is also called when the activity returns from the background.
onRestart()
onRestart() is called after the activity has been stopped. It is used when the activity is about to start
again. This method is always followed by onStart().
onResume()
onResume() is called when the activity starts interacting with the user. At this stage, the activity is at
the top and fully active. This method is always followed by onPause() when the activity goes into
the background.
onPause()
onPause() is called when the activity is going into the background but is still partially visible. This
happens when another activity comes in front. Heavy processing should be avoided in this method.
When the user returns, onResume() is called again.
onStop()
onStop() is called when the activity is no longer visible to the user. It comes after onPause(). If the
user returns, onRestart() is called, and if the activity is closed, onDestroy() is called. In some low
memory situations, this method may not be called.
onDestroy()
onDestroy() is the final method called before the activity is destroyed. It is called when the activity is
finished by the user or when the system removes it to free memory.
Flow of Activity Lifecycle
When the activity starts, it goes through onCreate(), onStart(), and onResume(). When another
activity comes in front, onPause() is called. If the activity becomes invisible, onStop() is called. If the
user returns, onRestart(), onStart(), and onResume() are called again. When the activity is closed,
onDestroy() is called.