Activity and Its Lifecycle
Activity and Its Lifecycle
An Activity is an application component that provides you an interface to perform the operations.
More simply, you can say that an Activity is a screen which is presented to the user by an
application. An Activity can be a dialler screen, Google Maps screen, etc. When you open your
application the very first screen that appear at front of you, is called a default Activity (or Main
Activity). There can be more than one loosely coupled Activities in your application. Generally,
as the complexity of your application increases, the need of number of Activities increases
proportionally.
An Activity can start another Activity to perform some actions. If it does so then system stops the
current Activity and preserves it in a stack (called Back Stack). When a new Activity starts, system
run it on to the top of the stack and takes the user’s focus. When a user taps on the back button,
then the current Activity pops out of the stack and relinquish the user’s focus and gets destroyed;
then previous Activity comes into the focus. You can understand this explanation with the help of
a diagram shown in Figure 4.1.
Creating an Activity:
To understand this in detail consider the following example.
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
Remember that an Activity should be registered in the Manifest.xml file after its creation. To
register an Activity in the Manifest file, use the <activity> tag within <application> tag. The
<activity> tag consists of many attributes such as name, label, themes, and permissions, to add the
properties of the Activity. If you don’t specify the <activity> tag in your Manifest file then the
Activity will not display on the screen, and system will show a run time error when you run the
application. An <activity> tag contains an <intent-filter> tag to specify the Intents that can start
the Activity. Intents are described in detail the upcoming lectures. Example is given below.
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
To have proper understanding of the working of an Activity you need to first know lifecycle of
the Activity.
Activity Lifecycle:
From its creation to its conclusion, an Activity goes through many stages of its life
such as create, start, pause, stop, etc. This journey of Activity to passing through
different stages of life called lifecycle of the Activity. Every stage of the Activity
lifecycle has a specific method associated to it, called call-back method. When an
Activity stops and another starts, it means a call-back method is called for each
Activity. The lifecycle of an Activity is managed by the Android run time system.
Generally, an Activity can remain in following three states:
1. Resumed: This state is also called running state. In this state an Activity remains
in the foreground of the screen and has the focus of the user.
2. Paused: When another Activity comes at the foreground of the Activity, then
Activity exists in paused state. In this state Activity remains alive and partially
visible but doesn’t have focus of the user. When an Activity remains alive, it
means the object of the Activity still holds in the memory and carries its
involvement with the window manager.
3. Stopped: When an Activity goes into the background due to another Activity
then it remains into the stopped state. In this state, while an Activity remains
alive, but it relinquishes the involvement with window manager. This means it
is no longer visible to the user in the foreground or background.
Figures 4.2.1 and Figure 4.2.2 is depicting the state diagrams to represent the
different stages of the Activity life cycle with different call-back methods:
Figure 4.2.1: A simplified illustration of Activity Lifecycle
(Source: http://developer.android.com/training/basics/activity-lifecycle/starting.html)
Figure 4.2.2: Activity Lifecycle
(Source: http://developer.android.com/guide/components/activities.html)
Following table is showing each lifecycle call-back method with details such as name of the
method, description of the method, what method is called after the specified method, method is
killable or not, etc.
Table 4.1: A summary of the activity lifecycle's call-back methods
(Source: http://developer.android.com/guide/components/activities.html)
Method Description Killable Next
after?
onCreate() Called when the activity is first created. In the onCreate() No onStart()
method, perform basic application startup logic that
happens only once for the entire life of the activity. This
is where you should do all your normal static set up such
as create views, bind data to lists, and so on.
onRestart() Called after the activity has been stopped, just prior to it
No onStart()
being started again.
onStart() When the activity enters the Started state, the system No onResume()
invokes onStart(). This call makes the activity visible to
the user as the app prepares for the activity to enter the
foreground and become interactive. For example, this
method is where the code that maintains the UI is
initialized.
onResume() When the activity enters the Resumed state, it comes to No onPause()
the foreground, and the system invokes the onResume()
callback. This is the state in which the app interacts with
the user. The app stays in this state until something
happens to take focus away from the app, such as the
device receiving a phone call, the user navigating to
another activity, or the device screen turning off.
onPause() The system calls this method as the first indication that onResume()
Yes
the user is leaving your activity, though it does not or
always mean the activity is being destroyed. It indicates onStop()
that the activity is no longer in the foreground, but it is
still visible if the user is in multi-window mode.
onStop() When your activity is no longer visible to the user, it Yes onRestart()
enters the Stopped state, and the system invokes the or
onStop() callback. This can occur when a newly onDestroy()
launched activity covers the entire screen. The system
also calls onStop() when the activity finishes running
and is about to be terminated.
From the Stopped state, the activity either comes back to
interact with the user, or the activity is finished running
and goes away. If the activity comes back, the system
invokes onRestart(). If the Activity is finished running,
the system calls onDestroy().
onDestroy() onDestroy() is called before the activity is destroyed. Yes Nothing
The system invokes this callback for one of two reasons:
1. The activity is finishing, due to the user
completely dismissing the activity or due to
finish() being called on the activity.
2. The system is temporarily destroying the activity
due to a configuration change, such as device
rotation or entering multi-window mode.
In the table column “Killable after?” shows that system can kill the Activity after execution of
specified method in the row. There are three call-back methods of an Activity’s lifecycle after that
an Activity can be killed: onPause(), onStop() and onDestroy(). In the shortage of memory, the
Android runtime system can kill the Activity in the paused state without calling the onStop() and
onDestroy() methods.
Following skeleton code is showing each basic call-back method of Activity:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;