Introduction To Activities: Managing The Activity Lifecycle
Introduction To Activities: Managing The Activity Lifecycle
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..
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.
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 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.
7. onDestroy()
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
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
case 2
case 3
back pressed
case 1
A => D => C => B => A => D => C => B => A => App Close
case 2
case 3
android:launchMode="singleTop"
case 1
case 2
case 3
back pressed
case 1
case 2
case 3
B =>A
android:launchMode="singleTask"
case 1
case 2
case 3
back pressed
case 1
App Close
case 2
App Close
case 3
App Close
android:launchMode="singleInstance"
case 1
case 2
case 3
back pressed
case 1
case 2
case 3
B => A => App Close
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.