What is an Activity
An Activity on android is a single screen which holds the content and that data through with the user interacts with. This is done using Activity
class, which creates the UI and displays it on the screen, so a user can interact with it.
An Activity can be either a fullscreen, or can be minimized like a floating activity (like Picture in Picture PiP mode), a multi window or embedded into other activities.
Activity Lifecycle
Each Activity in any android app , have a lifecycle that it follows. It basically consist of different methods which from creation of the activity to destruction of the activity handles, everything.
Below is the image from official android documentation.
Various methods for handling Activity’s Lifecycle
-
onCreate(): This is the very first method which is called when an app/activity is started. At this point, nothing is visible to the user, activity is being created, data is being binded to the lists etc. E.g When you open any app if you ever see a small split second white delay before seeing the UI, then that’s where
onCreate()
is being called. -
onStart(): This is called after
onCreate()
when an app starts or after onRestart() when a user moves back to this activity. In this state, the UI becomes visible to use user, but still the user is unable to interact with the UI. -
onResume(): This is called after
onStart()
or afteronPause()
. In this state, the UI is completely visible to the user and user can now interact with the UI, by clicking buttons, entering some data in the input fields. E.g. When an app is fully loaded, you can click on the buttons. -
onPause(): This method is called as soon as the user is about to move to some another activity. E.g. If a user is on profile screen and wants to move to Edit Profile Screen, the first
Profile Screen
is paused, before being stopped, beforeProfile Edit Screen
is created. -
onStop(): This is called after
onPause()
. In this state, the activity is no longer being visible nor the User can interact with it anymore. From here, the user can go to three states onlyonCreate()
i.e. when activity was killed by system and is recreated,onRestart()
, that is when the user moves back to this activity andonDestroy()
, when user completely kills the activity. However, in this case the activity is not completely destroyed or removed from the memory as this can be resumed again, seeonRestart()
method below. -
onRestart(): When a user moves back to already stopped activity (NOT DESTROYED/KILLED ACTIVITY), like going back to previous screen, then
onRestart()
is called. This function will again go back toonStart()
->onResume()
state, hence resuming the already paused activity. -
onDestroy(): This is the final phase of the activity. In this phase, the acitivty is completely killed and can’t be resumed unless it’s recreated from the start using
onCreate()
method. All memory is freed by the system. This usually happens when you completely close the app by going to recents screen and then closing the app from there. This is the final phase of an activity.
NOTE: If you want to save any data before moving to a new activity, consider doing it in onPause()
state as it still have all the data intact. However make sure that this process is not too lengthy, else the new activity will not load unless the save operations in onPause()
is completed.
So, this was everything about Activities in android.