Lifecycle of an Android Activity
Android has four basic components – Activity, Service, ContentProvider, and BroadcastReceiver, among which Activity is used very frequently and handles almost all interface interactions. Now, let's explore the lifecycle of an activity following the Figure 10.3.
-
onCreate()
indicates that the activity is being created. It is the first method through an activity's lifecycle and is where you should do the initialization. -
onStart()
indicates that the activity is being started and is visible to users. -
onRestart()
indicates that the activity is being restarted, and should be called when the activity changes from invisible to visible. For example, when users press the Home button to switch to the desktop, or open a new activity, the current activity will be stopped. When the current activity returns to the front, theonRestart()
method will be called. -
onResume()
indicates that the activity has been created and users can operate and interact on the interface. -
onPause()
indicates that the activity is paused, and usuallyonStop()
will be called immediately after. If users quickly return to the current activity,onResume()
will be called. -
onStop()
indicates that the activity is about to stop. It is no longer visible to users and is running only in the background. -
onDestroy()
indicates that the activity is about to be destroyed. This is the last method executed in an activity's lifecycle and is where you should reclaim or release resources.