Activity and Its Lifecycle Management

Introduction

An Activity is a UI controller component provided by the Android framework that can be used as an entry point into the application. Activity has a lifecycle associated with it, and the framework provides several callback functions to execute pieces of code when these events are triggered. An Activity has an associated layout file in which we can define the user interface. An application can contain multiple activities based on the use case, but a single activity remains in the active state at any given moment. We can start one activity from another activity and share the information with the calling activity to provide a seamless and unified experience for the user.

Creating and using activities

In this section, we’ll create an activity called ExampleActivity, which shows a text view with a counter that increments each time the activity is resumed. We need to follow the steps mentioned below to be able to create and use an Activity.

Register in the manifest

We must register its identity in the application’s manifest file to use an Activity. Each application has its own manifest file named AndroidManifest.xml The manifest files contain several tags, including a global tag named <application>. All activities that are supposed to belong to this application must be defined under this tag. For the sake of this tutorial, let’s name our activity ExampleActivity. To register our activity in the manifest, we make use of the <activity> tag as shown below.

Get hands-on with 1200+ tech skills courses.