DEV Community

Collins Etemesi
Collins Etemesi

Posted on

Android Activity Lifecycle

In an Android app, the Android system uses activities to run code. The activity lifecycle is the different states that an activity can go through, from when the activity first initializes to its destruction.
Image description
The Activity class provides 7 callback methods that let the activity know when a state changes: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy().

onCreate()
The onCreate() lifecycle method is called once, just after the activity initializes—when the OS creates the new Activity object in memory. After onCreate() executes, the activity is considered created.

onStart()
The onStart() lifecycle method is called just after onCreate(). After onStart() runs, your activity is visible on the screen to the user. Unlike onCreate(), which is called only once to initialize your activity, onStart() can be called by the system many times in the lifecycle of your activity.

onResume()
This method is called when the activity will start interacting with the user. The user starts interacting with your app for this to happen.

onPause()
Always called after onResume() and when the user is no longer actively interacting with the activity but the activity is still visible on the screen.

onStop()
This is called when the activity is no longer visible on the screen to the user.

onRestart()
Called when the current activity is being redisplayed to the user. From the Stopped state, if the activity comes back to interact with the user the system invokes onRestart().

onDestroy()
is called before the activity is destroyed. The system invokes this callback either because, the user dismisses the activity, or there is a configuration change such as device rotation.

Thank you for reading this article. Happy Coding.

Top comments (0)