- Android Development Tutorial
- Android Core Concepts
- Installing Android Studio
- Your First Android App
- Android Project Overview
- Android Activity
- Android View and ViewGroup
- Android Layout
- Android Fragment
- Android ActionBar
- Article TOC Test
- Android Toast
- Android TextView
- Android Buttons
- Android Web Apps Using Android WebView
Android Activity
Jakob Jenkov |
An Android activity is one screen of the Android app's user interface. In that way an Android activity is very similar to windows in a desktop application. An Android app may contain one or more activities, meaning one or more screens. The Android app starts by showing the main activity, and from there the app may make it possible to open additional activities.
Activity Life Cycle
Any Android activity goes through a certain life cycle during its life inside the Android app. This life cycle is illustrated here:

When an Android app is first started the main activity is created. The activity then goes through 3 states before it is ready to serve the user: Created, started and resumed.
If the main activity can open any other activities (screens) these activities will go through the same 3 states when they are opened.
If an activity A opens another activity B, then activity A will be paused. This means that activity A goes into the paused state. When the user clicks the back button and returns to activity A, activity A returns to the resumed state.
If the user returns to the home screen of the Android device, all activities will be paused and then stopped. If the user then returns to the app, the activities will go through the started and then resumed states. If the Android device needs the memory that the Android app occupies in the device's memory, then it may completely destroy the app, meaning the Android activities go into the destroyed state.
Note that the states always follows the arrows in the diagram. An activity cannot jump from created to resumed directly. An activity will always go through created, started and resumed in that sequence. An activity may change from resumed to paused an back, and from paused to stopped and back to started, but never from stopped directly to resumed.
Activity Life Cycle Methods
All activities in your Android apps are represented by an activity class. These activity classes are subclasses
of android.app.Activity
. The Activity
class contains a set of methods that corresponds
to the life cycle states an activity can be in. These methods are:
onCreate()
onStart()
onRestart()
onResume()
onPause()
onStop()
onDestroy()
When activity is to transition into one of the life cycle states, the corresponding life cycle method is called
on the Activity
subclass instance. When the life cycle method finishes the activity is assumed to
be in the new life cycle state. For instance, once the onCreate()
life cycle method returns, the
activity is assumed to be in the created state.
As mentioned earlier, all Android activities are subclasses of the Activity
class. The
Activity
class contains the above listed life cycle methods. If your activity class needs to do
something special when it changes life cycle state, you can override the corresponding life cycle method
in your Activity
subclass. In fact, your activity classes will pretty much always need to
override at least one of these life cycle methods. For instance, to initialize the UI, or load / save data etc.
A small note: The onRestart()
life cycle method is called when the activity transitions from stopped
to started. After onRestart()
the life cycle method onStart()
is called, and the activity
is considered started again.
Here is an example Activity
subclass called MyActivity
which overrides all of the
above life cycle methods:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); } @Override protected void onStart() { super.onStart(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } }
Notice how each of the life cycle methods call the corresponding method in the superclass. If your
Activity
subclass overrides one of the life cycle methods, it must call the corresponding
method in the superclass inside the overridden method.
Creating an Activity
When you create a new Android project in Android Studio the project will contain an Android Activity class already (unless you choose that is should not create an activity).
An activity is a subclass of the Android class android.app.Activity
. Here is an example of
how an Activity
subclass could look:
package com.jenkov.myfirstandroidapp; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MyFirstAndroidActivity extends Activity { }
This Activity
subclass does not do anything interesting. It just extends android.app.Activity
.
When you create a blank activity in Android Studio the Activity
subclass which Android Studio generates
for you looks like this:
package com.jenkov.myfirstandroidapp; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MyFirstAndroidActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_first_android); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my_first_android, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Notice how this Activity
subclass overrides the life cycle methods onCreate()
,
and the two other methods onCreateOptionsMenu()
and onOptionsItemsSelected()
.
All of these methods will be covered in more detail later, so I won't say more about them here. What is
important to know at this time is that the Activity
subclass overrides methods inherited
from the Activity
class in order to do anything interesting.
Creating an Activity in Android Studio
When you create a new project in Android Studio, the new project dialog will ask you if you want to create
an activity as part of the project. If your app only needs a single activity (a single screen), you do not
need to create more Activity
subclasses.
However, if your app needs more than one activity, you can add an activity to your project via the File menu, by choosing the "New..." menu item. Here is where you find that menu item:

In the menu that opens, click on the "Activity" menu item, and choose what kind of activity you want to create. Here is how that looks in Android Studio:

Activity XML Layout Files
An activity may have an associated XML layout file. The XML layout file specifies how the GUI components in the activity are to be rendered relative to each other. In the screenshot below I have marked the activity class and its associated layout XML file in Android Studio's project browser:

You are not forced to have a layout XML file for each activity. An activity can also create the layout programmatically
(typically from inside the onCreate()
method of the Activity
subclass). However,
in most cases it is easier to use a layout XML file. The layout XML file enables you to change the layout without
having to change the code inside your Activity
subclass.
Layout XML files will be covered in more detail in a later text in this Android development tutorial trail, so I will not talk more about them here.
onCreate()
The Activity
life cycle method onCreate()
is the first method to get called by the
Android operating system when an activity is created. Inside this method you should initialize the activity.
Initialization of the activity includes initialization of the user interface components, and any other resources
the activity needs to do its job.
Actually, some resources should be initialized in onStart()
or onResume()
and freed
in onStop()
or onPause()
. It depends on how expensive it is to keep the resource open
when it is not used, and whether the resource should remain open when not used.
Here is how the default onCreate()
implementation looks for an activity generated by Android Studio:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_first_android); }
First, super.onCreate()
is called. Activity life cycle methods should always call their corresponding
methods in the superclass.
Second, setContentView()
is called. This method specifies what view is to
be displayed inside the activity. An activity can only contain a single root view, but this root view may have
multiple views nested inside it. Nesting views inside ViewGroups (which are also views) is how you compose more
complex UIs.
The parameter passed to setContentView()
is a constant pointing to the layout XML file
for the activity. In this case it is the constant R.layout.activity_my_first_android
. This constant
was generated by Android Studio when I created the activity class. In fact, all constants inside the R
class are generated by Android Studio or the Android SDK based on files located in your project
(layout files, string files, images etc.).
The layout XML file the constant R.layout.activity_my_first_android
points to is located in
the directory app/src/main/res/layout
, and is called activity_my_first_android.xml
.
This screenshot illustrates the location of this file in the Android Studio project:

Initializing The Activity UI
After the call to setContentView()
you could initialize the GUI components of the activity.
Here is an example onCreate()
implementation which initializes a single TextView
component:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_first_android); TextView textView = (TextView) findViewById(R.id.the_text_view); textView.setText("I have changed the text!!"); }
The findViewById()
method is inherited from the Activity
class. The findViewById()
method takes an id of the View
component to find as parameter. The id is a constant which is also located
inside the R
class. In this case in R.id.the_text_view
. This constant is generated based
on the configuration of the TextView
component inside the layout XML file for the activity. Here is
the layout XML file (activity_my_first_android
):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyFirstAndroidActivity"> <TextView android:id="@+id/the_text_view" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Notice the TextView
element inside the RelativeLayout
element. The TextView
element has an attribute named android:id
. The value of this attribute is @+id/the_text_view
. This attribute value instructs the Android SDK to create an id constant named the_text_view
.
It is the +
after the @
which tells the Anroid SDK to create the constant. If the
+
was not there, the attribute value would be interpreted as a reference to an id of a constant
created elsewhere.
It is this generated id constant which is used as parameter to the findViewById()
method call shown
earlier. Since the constant is an id (@+id/...
), the constant is generated into the R.id
collection of constants.
Once you have obtained a reference to the TextView
component you can all any initialization methods
on it you need. In the example above the setText()
method was called to change the text displayed
by the TextView
.
Tweet | |
Jakob Jenkov |