0% found this document useful (0 votes)
26 views9 pages

Activity Life Cycle

The document outlines the Activity Lifecycle in Android, detailing the four main states: Running, Paused, Resumed, and Stopped. It describes the callback methods associated with these states, including onCreate(), onPause(), onStop(), and others, which manage the transitions between states. Additionally, it discusses the use of Logcat for debugging and provides code examples demonstrating the lifecycle methods in action.

Uploaded by

dreamtodare6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Activity Life Cycle

The document outlines the Activity Lifecycle in Android, detailing the four main states: Running, Paused, Resumed, and Stopped. It describes the callback methods associated with these states, including onCreate(), onPause(), onStop(), and others, which manage the transitions between states. Additionally, it discusses the use of Logcat for debugging and provides code examples demonstrating the lifecycle methods in action.

Uploaded by

dreamtodare6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Activity Lifecycle

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.

Android Activity Methods

• Android activities go through four states during their entire lifecycle.

• 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:

View logs with Logcat

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.i display information messages/ expected log messages.


• Log.d display debug messages.

Log.v display all log messages.


We have used Log class which is used to printout message in Logcat. One of the important use of Log is in
debugging.

Log.d(tag, message);

we use a TAG which represents the name of the class.

private static final String TAG = MainActivity.class.getSimpleName();

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;

public class MainActivity extends AppCompatActivity {

@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:

 first onCreate() method was called when activity was created

 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 open the app it will go through below states:

onCreate() –> onStart() –> onResume()

 When you press the back button and exit the app

onPaused() — > onStop() –> onDestory()

 When you press the home button

onPaused() –> onStop()

 After pressing the home button, again when you open the app from a recent task list
onRestart() –> onStart() –> onResume()

 After dismissing the dialog or back button from the dialog

onResume()

 If a phone is ringing and user is using the app

onPause() –> onResume()

 After the call ends

onResume()

 When your phone screen is off

onPaused() –> onStop()

 When your phone screen is turned back on

onRestart() –> onStart() –> onResume()

So these are some of the situations when your app goes through various states.

You might also like