A single screen of the UI is an activity in Android. An activity serves as an app’s entry point and provides a UI to handle user interactions with the screen. For instance, an email app can have one activity to display new emails, another activity to read emails, and yet another activity to compose new emails. We make one of these activities as the main activity that’s run when the app is launched. The activities work together; however, each activity is independent of the others. Let’s learn about Android activities and their life cycle methods.

Android activities

Unlike most programming paradigms where apps start to run from a main() method, Android initiates an activity’s code by invoking callback methods that represent various stages of an activity’s life cycle. Mobile apps are different from desktop apps because the start of the user’s interaction with the mobile app is nondeterministic. For example, if we open an email app from our home screen, we usually discover a list of emails. However, if we launch the same email app from a saved contact, we can see the email composing screen.

The Activity class

The Activity class is a key component in an Android app that facilitates user interaction with the app. Whenever an app tries to initiate another app, the first app invokes an activity in the second app instead of the app. Therefore, the activity being invoked becomes the entry point for the app. In Kotlin, we can implement activities as subclasses of the Activity class.

An activity supplies a window where our app draws its UI. Generally, each activity implements one screen within the app. For instance, one activity of an app can implement a download image operation, whereas another activity can implement an open gallery operation to view the downloaded image.

Activity life cycle

During its life cycle, each activity undergoes various states. We use system callbacks to handle state transitions. The following figure shows the life cycle methods for an activity.

Get hands-on with 1400+ tech skills courses.