0% found this document useful (0 votes)
101 views

Introduction To Activities: Managing The Activity Lifecycle

The document discusses the Android activity lifecycle and launch modes. It describes the key callback methods like onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy() that are used to manage an activity's state as it moves between active, inactive, paused, and stopped states. It also explains how to define launch modes for activities in the Android manifest file or by setting intent flags, and provides examples of how different activities would behave using each launch mode.

Uploaded by

Ruchi Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Introduction To Activities: Managing The Activity Lifecycle

The document discusses the Android activity lifecycle and launch modes. It describes the key callback methods like onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy() that are used to manage an activity's state as it moves between active, inactive, paused, and stopped states. It also explains how to define launch modes for activities in the Android manifest file or by setting intent flags, and provides examples of how different activities would behave using each launch mode.

Uploaded by

Ruchi Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Introduction to Activities

The Activity class is a crucial component of an Android app, and the way activities are launched and put together is a
fundamental part of the platform's application model. Unlike programming paradigms in which apps are launched with
a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that
correspond to specific stages of its lifecycle.

 the activity serves as the entry point for an app's interaction with the user. You implement an activity as a
subclass of the Activity class.

 An activity provides the window in which the app draws its UI. This window typically fills the screen, but may
be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in
an app.

 Most apps contain multiple screens, which means they comprise multiple activities. Typically, one activity in
an app is specified as the main activity, which is the first screen to appear when the user launches the app..

Managing the activity lifecycle

Over the course of its lifetime, an activity goes through a number of states. You use a series of callbacks to handle
transitions between states. The following sections introduce these callbacks.

1. onCreate()

You must implement this callback, which fires when the system creates your activity. Your implementation should
initialize the essential components of your activity: For example, your app should create views and bind data to lists
here. Most importantly, this is where you must call setContentView() to define the layout for the activity's user
interface.

When onCreate() finishes, the next callback is always onStart().

2. onStart()

As onCreate() exits, the activity enters the Started state, and the activity becomes visible to the user. This callback
contains what amounts to the activity’s final preparations for coming to the foreground and becoming interactive.

3. onResume()

The system invokes this callback just before the activity starts interacting with the user. At this point, the activity is at
the top of the activity stack, and captures all user input. Most of an app’s core functionality is implemented in
the onResume() method.

The onPause() callback always follows onResume().


4. onPause()

The system calls onPause() when the activity loses focus and enters a Paused state. This state occurs when, for example,
the user taps the Back or Recents button. When the system calls onPause() for your activity, it technically means your
activity is still partially visible, but most often is an indication that the user is leaving the activity, and the activity will
soon enter the Stopped or Resumed state.

An activity in the Paused state may continue to update the UI if the user is expecting the UI to update. Examples of such
an activity include one showing a navigation map screen or a media player playing. Even if such activities lose focus,
the user expects their UI to continue updating.

You should not use onPause() to save application or user data, make network calls, or execute database transactions.
For information about saving data, see Saving and restoring activity state.

Once onPause() finishes executing, the next callback is either onStop() or onResume(), depending on what happens
after the activity enters the Paused state.

5. onStop()

The system calls onStop() when the activity is no longer visible to the user. This may happen because the activity is
being destroyed, a new activity is starting, or an existing activity is entering a Resumed state and is covering the
stopped activity. In all of these cases, the stopped activity is no longer visible at all.

The next callback that the system calls is either onRestart(), if the activity is coming back to interact with the user, or
by onDestroy() if this activity is completely terminating.

6. onRestart()

The system invokes this callback when an activity in the Stopped state is about to restart. onRestart() restores the state
of the activity from the time that it was stopped.

This callback is always followed by onStart().

7. onDestroy()

The system invokes this callback before an activity is destroyed.

This callback is the final one that the activity receives. onDestroy() is usually implemented to ensure that all of an
activity’s resources are released when the activity, or the process containing it, is destroyed.

This section provides only an introduction to this topic. For a more detailed treatment of the activity lifecycle and its
callbacks, see The Activity Lifecycle.
1. When App Open

Activity 1 onCreate() Called


Activity 1 onStart() Called
Activity 1 onResume() Called

2. When Phone Sleep

Activity 1 onPause() Called


Activity 1 onStop() Called

3. When Phone Wake up From Sleep

Activity 1 onReStart() Called


Activity 1 onStart() Called
Activity 1 onResume() Called

4. When Home button press

Activity 1 onPause() Called


Activity 1 onStop() Called

5. Again back to App

Activity 1 onReStart() Called


Activity 1 onStart() Called
Activity 1 onResume() Called

6. When Orientation Changed

Activity 1 onPause() Called


Activity 1 onStop() Called
Activity 1 onDestroy() Called
Activity 1 onCreate() Called
Activity 1 onStart() Called
Activity 1 onResume() Called

7. When App Goes to recent task and kill


a. When go to task bar
Activity 1 onPause() Called
Activity 1 onStop() Called

b. When kill app

Activity 1 onDestroy() Called

8. When open Activity without finish

Activity 1 onPause() Called


Activity 2 onCreate() Called
Activity 2 onStart() Called
Activity 2 onResume() Called
Activity 1 onStop() Called

8. When open Activity with finish

Activity 1 onPause() Called


Activity 2 onCreate() Called
Activity 2 onStart() Called
Activity 2 onResume() Called
Activity 1 onStop() Called
Activity 1 onDestroy() Called

9. When Back from Activity 2 (if open without finish)

Activity 2 onPause() Called


Activity 1 onReStart() Called
Activity 1 onStart() Called
Activity 1 onResume() Called
Activity 2 onStop() Called
Activity 2 onDestroy() Called

9. When Back from Activity 2 (if open with finish)

Activity 2 onPause() Called


Activity 2 onStop() Called
Activity 2 onDestroy() Called

Launch modes:
It is used to instructs Android system on how to launch a particular activity.
Two ways to define launchMode for an Activity:
1. In Manifest file
2. Using Intent Flags

Defining launch modes in Manifest File:


“android:launchMode” is an attribute defined inside <activity .../> tag in AndroidManifest.xml

<activity android:launchMode = [“standard” | “singleTop” | “singleTask” | “singleInstance”] ../>


Launch Mode Test Cases

test 1 All Activities have android:launchMode="standard"


case 1

A => B => C => D => A =>B => C => D =>A

case 2

A => B => C =>B

case 3

A => B => B =>B

back pressed

case 1

A => D => C => B => A => D => C => B => A => App Close

case 2

B=> C => B =>A => App Close

case 3

B => B => B =>A => App Close

android:launchMode="singleTop"

case 1

A => B => C => D => A =>B => C => D =>A

case 2

A => B => C =>B

case 3

A => B => B => B (Same Top Activity launch only once)

back pressed

case 1

A => D => C => B => A => D => C => B => A

case 2

B=> C => B =>A

case 3
B =>A

android:launchMode="singleTask"

case 1

A => B => C => D => A =>B => C => D =>A

case 2

A => B => C =>B

case 3

A => B => B => B

back pressed

case 1

App Close

case 2

App Close

case 3

App Close

android:launchMode="singleInstance"

case 1

A => B => C => D => A =>B => C => D =>A

case 2

A => B => C =>B

case 3

A => B => B => B (Same Top Activity launch only once)

back pressed

case 1

A => D => C => B => App Close

case 2

B => C => A => App Close

case 3
B => A => App Close

Defining launch modes using Intent Flags:

Intent i = new Intent(FirstActivity.this, FirstActivity.class);


i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

1. “FLAG_ACTIVITY_NEW_TASK”:
Now, putting it straight — this is just as same behaviour as
launchMode — singleTask.

2. “FLAG_ACTIVITY_SINGLE_TOP”:
this is same as launchMode — singleTop. You have to set the Intent Flag as follows:

3. “FLAG_ACTIVITY_CLEAR_TOP”:
There are two scenarios of use here:
a) All Activities in the same Task:
As expected, this flag will clear all the Activities on the top of Intended Activity in the stack and bring it to Foreground.

b) Activities spread across different tasks:


If this flag is set along with FLAG_ACTIVITY_NEW_TASK then the system will bring the required Task that
contains the intended Activity to Foreground, and clear all the Activities on the top of that Activity.

You might also like