An activity is one of the fundamental components of an Android app and typically represents a single screen with a user interface, such as a login screen, a list of messages or a settings page. Each activity is a class that inherits from Activity or, most often, from AppCompatActivity or ComponentActivity, and it must be registered in the app’s AndroidManifest.xml. When the user opens the app from the home screen, the system starts the activity specified as the app’s entry point, and from there the app can open other activities by means of intents.
An activity has a lifecycle controlled by the Android system. Through methods such as onCreate(), onStart(), onResume(), onPause(), onStop() and onDestroy(), the app is notified when the screen is created, becomes visible, loses focus or is closed, so that resources can be released and data saved at the right moment. Many modern apps use only one or a few activities and build the individual screens with fragments or Jetpack Compose, but the activity remains the element that connects the app’s user interface to the system.
An activity in app development refers to a single screen in an Android application that acts as an interactive window for the user. Each activity is responsible for handling user interactions and presenting data, which enables developers to create dynamic and engaging user experiences. Activities can work together by passing data between one another, which ensures seamless navigation and functionality in the app.
More about Activity
An activity is a central component in Android application development that represents a single screen with a user interface. Each activity is a self-contained unit that handles user interactions and presents data, which enables developers to create dynamic and engaging user experiences. Activities are fundamental building blocks of Android apps, and they act as entry points through which users interact with the app’s functions.
Technically, an activity is a subclass of the Activity class in the Android SDK, and it works by handling lifecycle methods that control how the activity is created, started, stopped and destroyed. When an activity is started, it passes through a series of lifecycle stages that enable developers to manage resources efficiently and respond to user interactions. These include methods such as onCreate(), onStart(), onResume(), onPause(), onStop() and onDestroy(), each of which plays a role in ensuring a smooth and responsive user experience.
Activities can be presented in different ways, each supporting different types of interaction:
-
Full screen: Here, the activity acts as a standalone screen that handles user interactions and presents data. This is the most common form, in which the user navigates between different screens in an app.
-
As a dialog: With a dialog theme, the activity is presented as a dialog box floating above the current screen. This is sometimes used to show important messages or request user input without leaving the current context, although dialogs are more often implemented with dialog fragments or Compose.
-
Immersive mode: The activity takes full control of the screen and hides system bars such as the status bar and navigation bar to provide an immersive experience. This is ideal for games or media playback, where focus on the content is crucial.
One of the greatest strengths of activities is their ability to handle complex user interactions and navigation patterns. Android’s activity model supports a number of navigation patterns, such as the back stack, which allows users to navigate back to previous screens, creating an intuitive and predictable user experience. In addition, activities can share data and communicate with one another by means of intents, which enables seamless integration between different parts of an app.
Activities are also designed to be resource-efficient. Android’s lifecycle methods enable developers to optimise resource consumption by releasing unnecessary resources when an activity is not visible and reloading them when it returns to the foreground. This helps ensure that apps remain responsive and efficient, even on devices with limited resources.
In practice, activities have shaped the way Android apps are designed and developed. They provide a structured approach to handling user interactions and navigation patterns, which enables developers to create rich and engaging user experiences. Activities are at the core of many popular apps, where they are used to organise content, handle user input and navigate between different functions.
In summary, activities are an essential part of Android development that combines flexibility, efficiency and ease of use in a solution that enables developers to create powerful and intuitive apps. Their ability to handle complex lifecycles and navigation patterns makes them a central tool in modern app development, where the user experience and efficient resource management are crucial.
Frequently asked questions about Activity
What is an activity in Android?
An activity is one of the fundamental building blocks of an Android app. It typically represents a single screen with a user interface, such as a login screen, a list of messages or a settings page. The user interacts with the app through its activities.
Each activity is a class that inherits from Activity or, more often, AppCompatActivity or ComponentActivity, and it must be declared in the AndroidManifest.xml file. One activity is marked as the entry point, so that the system knows what to open when the user taps the app’s icon. Modern apps often have few activities and use fragments or Jetpack Compose to show different screens.
How does an activity's lifecycle work?
An activity passes through a series of states from the moment it is created until it is closed. Android calls specific methods when the state changes. onCreate is called when the activity is created, and this is where the user interface is set up. onStart and onResume are called when it becomes visible and ready for use.
When the user switches to another app or screen, onPause and onStop are called, and when the activity is closed completely, onDestroy is called. By placing code in the right methods, the app can, for example, stop a video when it is not visible and save data before it is closed. Incorrect handling can cause errors and waste resources.
How does an app switch between activities?
An app switches from one activity to another by means of an intent. An intent describes which activity should be opened and can contain data, such as the ID of a product that the new screen should show. The intent is sent with the startActivity method.
The new activity is placed at the top of a stack known as the back stack. When the user presses back, the top activity is closed, and the previous one is shown again. If the new screen needs to return a result, such as a selected image, the Activity Result API is used. Many modern apps, however, use a single activity and the Navigation component to switch between screens within it.
What happens to an activity when the screen is rotated?
When the user rotates the phone, the configuration changes, and by default Android destroys the current activity and creates it again. This makes it possible to load a different layout suited to the new orientation, but it also means that data held only in the activity can be lost.
To avoid this, a ViewModel is typically used, which survives configuration changes and retains data such as loaded lists. Small amounts of state, such as text in a field or a scroll position, can be saved with onSaveInstanceState or with rememberSaveable in Jetpack Compose. The same applies when the language, theme or screen size changes.
What is the difference between an activity and a fragment?
An activity is a self-contained component known to the system and declared in AndroidManifest.xml. It acts as a container for the app’s user interface and can be started directly, for example from a notification or another app.
A fragment is a smaller, reusable part of the user interface that always lives inside an activity. Several fragments can be shown in the same activity, such as a list and a detail view side by side on a tablet. Fragments have their own lifecycle, which is tied to the activity that hosts them. Many apps use few activities and many fragments or Compose screens.
What are launch modes for an activity?
Launch modes determine how a new activity is placed in relation to existing ones when it is started. By default, a new instance is created every time, even if the same activity is already open.
With singleTop, the activity is reused if it is already at the top of the stack, instead of creating a new one. With singleTask, only one instance exists, and activities above it are removed when it is opened again. singleInstance places the activity in its own task, and since Android 12, singleInstancePerTask allows one instance per task. Launch modes are specified in AndroidManifest.xml or with flags on an intent and are used, for example, to avoid duplicates when the app is opened from a notification.
Activity for dummies
An activity in an app is like a single screen on a mobile phone. Think of it as a page in a book. When an app is opened, a screen appears where something specific can be done, such as reading news, sending a message or looking at photos. Each of these screens is an “activity”. Switching from reading news to sending a message means switching from one activity to another. It is the way apps organise different functions and information, so that they are easy to navigate and use.