Android Activity – An Overview

by Mir Suhail

Activity is an Application component that provides a screen with which users can interact in order to do tasks, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.

An activity is a single, focused thing that a user can do. Almost all activities interact with a user, so the Activity class takes care of creating a window for you in which we can place our UI with setContentView(View). While activities are often presented to user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowsFloating set) or embedded inside of another activity (usingActivityGroup).

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the “main” activity, which is presented to user when launching application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, previous activity is stopped, but system preserves the activity in a stack (the “back stack”). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to basic “last in, first out” stack mechanism, so, when user is done with the current activity and presses Back button, it is popped from stack (and destroyed) and previous activity resumes.


Creating an Activity

To create an activity, we must create a subclass of Activity or an existing subclass of it. In our subclass, you need to implement callback methods that the system calls when activity transitions between various states of its life cycle, such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are:

  • onCreate():  We must implement this method. The system calls this when creating your activity. Within your implementation, you should initialize the essential components of your activity. Most importantly, this is where you must call setContentView() to define the layout for the activity’s user interface.
  • OnPause(): The system calls this method as the first indication that the user is leaving our activity (though it does not always mean activity is being destroyed). This is usually where we should commit any changes that should be persisted beyond current user session (because the user might not come back).

There are several other life cycle callback methods that you should use in order to provide a fluid user experience between activities and handle unexpected interruptions that cause our activity to be stopped and even destroyed.

Implementing a user interface

The user interface for an activity is provided by a hierarchy of views—objects derived from the View class. Each view controls a particular rectangular space within the activity’s window and can respond to user interaction. For example, a view might be a button that initiates an action when user touches it.

Android provides a number of ready-made views that we can use to design and organize our layout. “Widgets” are views that provide a visual (and interactive) elements for the screen, such as a button, text field, checkbox, or just an image. “Layouts” are views derived from ViewGroup that provide a unique layout model for its child views, such as a linear layout, a grid layout, or relative layout. We can also subclass the View and ViewGroupclasses (or existing subclasses) to create our own widgets and layouts and apply them to our activity layout.

The most common way to define a layout using views is with an XML layout file saved in your application resources. This way, we can maintain design of our user interface separately from source code that defines a activity’s behavior. We can set the layout as UI for our activity with setContentView(), passing resource ID for the layout like:

protected void onCreate(Bundle savedInstanceState) {

However, we can also create new Views in our activity code and build a view hierarchy by inserting new Views into a ViewGroup, then use that layout by passing rootViewGroup to setContentView().

Declaring the Activity in the manifest

We must declare your activity in the manifest file in order for it to be accessible to system. To declare your activity, open your manifest file and add an <activity> element as a child of the <application> element. For example:

<manifest... >
  <application... >
  </application... >

There are several other attributes that we can include in this element, to define properties such as the label for the activity, an icon for the activity, or a theme to style the activity’s UI. The android:name attribute is the only required attribute—it specifies the class name of the activity. Once you publish your application, we should not change this name, because if we do, we might break some functionality, such as application shortcuts.


Using intent filters

An <activity> element can also specify various intent filters—using the <intent-filter> element—in order to declare how other application components may activate it.

When we create a new application using Android SDK tools, stub activity that’s created for us automatically includes an intent filter that declares that the activity responds to “main” action and should be placed in “launcher” category. The intent filter looks like this:


The <action> element specifies that this is the “main” entry point to application. The <category> element specifies that this activity should be listed in system’s application launcher (to allow users to launch this activity).

Starting an Activity

We can start another activity by calling startActivity(), passing it an intent that describes the activity we want to start. The intent specifies either the exact activity we want to start or describes type of action we want to perform (and  system selects appropriate activity for us, which can be from a different application too). An intent can also carry small amounts of data to be used by the activity that is started.

When working within our own application, we‘ll often need to simply launch a known activity. We can do so by creating an intent that explicitly defines the activity we want to start, using the class name. For example, here’s how one activity starts another activity named ExampleActivity:

Intent intent = new
Intent(this, ExampleActivity.class);


However, our application might also want to perform some action, such as send an email, text message, or status update, using data from our activity. In this case, our application might not have its own activities to perform such actions, so we can instead leverage the activities provided by other applications on the device, which can perform the action for us. This is where intents are really valuable—we can create an intent that describes an action we want to perform and system launches the appropriate activity from another application. If there are multiple activities that can handle the intent, then user can select which one to use. For example, if we want to allow user to send an email message, we can create the following intent:

Intent intent = new Intent(Intent.ACTION_SEND);


The EXTRA_EMAIL extra added to the intent is a string array of email addresses to which an email should be sent. When an email application responds to this intent, it reads the string array provided in extra and places them in “to” field of the email composition form. In this situation, email application’s activity starts and when user is done, our activity resumes.

To learn more about

Contact Us

Leave a Reply

Your email address will not be published. Required fields are marked *

Mobile Research Apps

We have deep expertise in design and development of mobile research apps that work on both iOS and Android securely.

Contact us now

Popular Posts