Android Activity Lifecycle
Activity is one of the building blocks of Android OS. In simple words Activity is a
screen that user interact with. Every Activity in android has lifecycle like created, started,
resumed, paused, stopped or [Link] other words we can say Activity is a class pre-
written in Java Programming. An activity is the single screen in android. It is like window or
frame of Java. By the help of activity, you can place all your UI components or widgets in a
single screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.
Android Activity Lifecycle methods
Let's see the 7 lifecycle methods of android activity.
onCreate() – Called when the activity is first created
onStart() – Called just after it’s creation or by restart method after onStop(). Here Activity
start becoming visible to user
onResume() – Called when Activity is visible to user and user can interact with it
onPause() – Called when Activity content is not visible because user resume previous
activity
onStop() – Called when activity is not visible to user because some other activity takes
place of it
onRestart() – Called when user comes on screen or resume the activity which was stopped
onDestroy – Called when Activity is not in background
Detailed introduction on each of the method is stated as follows:
Activity Lifecycle in Android with Demo App
In Android, an activity is referred to one screen in an application. It is very similar to a single-window of any desktop
application. An Android app consists of one or more screens or activities.
Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when a new activity starts,
the previous one always remains below it. There are four stages of an activity.
1. If an activity is in the foreground of the screen i.e at the top of the stack, then it is said to be active or running. This is
usually the activity that the user is currently interacting with.
2. If an activity has lost focus and a non-full-sized or transparent activity has focused on top of your activity. In such a
case either another activity has a higher position in multi-window mode or the activity itself is not focusable in current
window mode. Such activity is completely alive..
3. If an activity is completely hidden by another activity, it is stopped or hidden. It still retains all the information, and as
its window is hidden thus it will often be killed by the system when memory is needed elsewhere.
4. The system can destroy the activity from memory by either asking it to finish or simply killing its process. When it is
displayed again to the user, it must be completely restarted and restored to its previous state.
________________________________________
Different Types of Activity Lifecycle States:
Activity have different states or it’s known as Activity life cycle. All life cycle methods aren’t required to override but it’s
quite important to understand them. Lifecycles methods can be overridden according to requirements.
LIST OF ACTIVITY LIFECYCLE METHODS OR STATES:
Activity Created: onCreate(Bundle savedInstanceState):
onCreate() method is called when activity gets memory in the OS. To use create state we need to override onCreate(Bundle
savedInstanceState) method. Bundle is a data repository object that can store any kind of primitive data and this object will
be null until some data isn’t saved in that.
• When an Activity first call or launched then onCreate(Bundle savedInstanceState) method is responsible to
create the activity.
• Whenever orientation (i.e. from horizontal to vertical or vertical to horizontal) of activity gets changed or when
an Activity gets forcefully terminated by any Operating System then savedInstanceState i.e. object of Bundle
Class will save the state of an Activity.
• It is best place to put initialization code.
Activity Started: onStart():
onStart() method is called just after its creation. In other case Activity can also be started by calling restart method i.e after
activity stop. So this means onStart() gets called by Android OS when user switch between applications. For example, if a
user was using Application A and then a notification comes and user clicked on notification and moved to Application B,
in this case Application A will be paused. And again if a user again click on app icon of Application A then Application A
which was stopped will again gets started.
Activity Resumed:.onResume():
Activity resumed is that situation when it is actually visible to user means the data displayed in the activity is visible to
user. In lifecycle it always gets called after activity start and in most use case after activity paused (onPause).
Activity Paused: onPause():
Activity is called paused when it’s content is not visible to user, in most case onPause() method called by Android OS
when user press Home button (Center Button on Device) to make hide.
Activity also gets paused before stop called in case user press the back navigation button. The activity will go in paused
state for these reasons also if a notification or some other dialog is overlaying any part (top or bottom) of the activity
(screen). Similarly, if the other screen or dialog is transparent then user can see the screen but cannot interact with it. For
example, if a call or notification comes in, the user will get the opportunity to take the call or ignore it.
Activity Stopped: onStop():
Activity is called stopped when it’s not visible to user. Any activity gets stopped in case some other activity takes place of
it. For example, if a user was on screen 1 and click on some button and moves to screen 2. In this case Activity displaying
content for screen 1 will be stopped.
Every activity gets stopped before destroy in case of when user press back navigation button. So Activity will be in stopped
state when hidden or replaced by other activities that have been launched or switched by user. In this case application will
not present anything useful to the user directly as it’s going to stop.
Activity Restarted: onRestart():
Activity is called in restart state after stop state. So activity’s onRestart() function gets called when user comes on screen or
resume the activity which was stopped. In other words, when Operating System starts the activity for the first time
onRestart() never gets called. It gets called only in case when activity is resumes after stopped state.
Activity Destroyed: onDestroy():
Any activity is known as in destroyed state when it’s not in background. There can different cases at what time activity get
destroyed.
First is if user pressed the back navigation button then activity will be destroyed after completing the lifecycle of pause and
stop.
In case if user press the home button and app moves to background. User is not using it no more and it’s being shown in
recent apps list. So in this case if system required resources need to use somewhere else then OS can destroy the Activity.
After the Activity is destroyed if user again click the app icon, in this case activity will be recreated and follow the same
lifecycle again. Another use case is with Splash Screens if there is call to finish() method from onCreate() of an activity
then OS can directly call onDestroy() with calling onPause() and onStop().
File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link] xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="[Link]">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</[Link]>
Activity Lifecycle Example:
In the below example we have used the below JAVA and Android topics:
JAVA Topics Used: Method Overriding, static variable, package, Inheritance, method and class.
Android Topic Used: We have used Log class which is used to printout message in Logcat. One of the important use of
Log is in debugging.
First we will create a new Android Project and name the activity as HomeActivity. In our case we have named our App
project as Activity Lifecycle Example.
package [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
[Link]();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
[Link]();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
[Link]();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
[Link]();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
[Link]();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
[Link]();
Log.d("lifecycle","onDestroy invoked");
}
}
Output:
You will not see any output on the emulator or device. You need to open logcat.
Now see on the logcat: onCreate, onStart and onResume methods are invoked.
Now click on the HOME Button. You will see onPause method is invoked.
After a while, you will see onStop method is invoked.
Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
Now click on the lifecycleactivity icon.
Now see on the logcat: onRestart, onStart and onResume methods are invoked.
If you see the emulator, application is started again.
Now click on the back button. Now you will see onPause methods is invoked.
After a while, you will see onStop and onDestroy methods are invoked.
The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.