Activity Life Cycle
Activity Life Cycle
Activity is one of the building blocks of Android OS. Activity is a screen that user interact
with. Every Activity in android has lifecycle like created, started, resumed, paused, stopped or
destroyed. These different states are known as Activity Lifecycle.
An activity can have four states, which are :
1. Running
2. Paused
3. Resumed
4. Stopped
1. Running State
An activity is in the running state if it’s shown in the foreground of the users’ screen. Activity is in the running state
when the user is interacting with it.
2. Paused State
When an activity is not in the focus but is still alive for the user, it’s in a paused state. The activity comes in this state
when some other activity comes in with a higher position in the window.
3. Resumed State
It is when an activity goes from the paused state to the foreground that is an active state.
4. Stopped State
When an activity is no longer in the activity stack and not visible to the users.
• These activities have callback methods() to describe each activity in each of the four stages.
• In Android, we have the following 7 callback methods that activity uses to go through the four states:
Understanding activities
1. onCreate()
The Android oncreate() method is called at the very start when an activity is created.
An activity is created as soon as an application is opened.
This method is used in order to create an Activity.
2.
3. onPause()
• The Android onPause() method is invoked when the activity doesn’t receive any user input and goes on hold.
• In the pause state, the activity is partially visible to the user.
• This is done when the user presses the back or home buttons.
• Once an activity is in the pause state, it can be followed by either onResume() or onStopped() callback
method.
4.
5.
6. onStop()
• The Android onStop() method is invoked when the activity is no longer visible to the user.
• The reason for this state is either activity is getting destroyed or another existing activity comes back
to resume state.
7.
Lab Experiments
Key Points:
The Logcat window in Android Studio helps you debug your app by displaying logs from your
device in real time—for example, messages that you added to your app with the Log class,
messages from services that run on Android, or system messages, such as when a garbage
collection occurs. When an app throws an exception. Log methods:
Log.d(tag, message);
The java.lang.Class.getSimpleName() returns the name of the underlying class as given in the source code.
Logcat present inside Android Monitor: Scroll up and you will notice three methods which were called: Activity
Created, Activity started and Activity resumed.
Demonstrate Activity Life Cycle in Lab
package com.example.dipak.activitydemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle", "onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
Output:
So this clears:
second onStart() method was called when activity start becoming visible to user
Finally onResume() method was called when activity is visible to user and user can interact with it
On your phone:
When you press the back button and exit the app
After pressing the home button, again when you open the app from a recent task list
onRestart() –> onStart() –> onResume()
onResume()
onResume()
So these are some of the situations when your app goes through various states.