0% found this document useful (0 votes)
7 views3 pages

Lec 9

The Android Activity Lifecycle consists of seven key methods: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(), which manage the state of an activity based on user interactions. Each method has specific roles, such as initializing the activity, handling visibility, and managing transitions between activities. The lifecycle methods can be called multiple times during the activity's existence, while onCreate() and onDestroy() are called at most once per application run.

Uploaded by

hnanalsyd97
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)
7 views3 pages

Lec 9

The Android Activity Lifecycle consists of seven key methods: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(), which manage the state of an activity based on user interactions. Each method has specific roles, such as initializing the activity, handling visibility, and managing transitions between activities. The lifecycle methods can be called multiple times during the activity's existence, while onCreate() and onDestroy() are called at most once per application run.

Uploaded by

hnanalsyd97
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/ 3

Android Activity Lifecycle

An activity is the single screen in android. It contains all


UI components in a single screen.
Android Activity Lifecycle is controlled by 7 methods:
onCreate()
onStart()
onResume() These methods are called by Android based on
the user interaction with the activity
onPause()
onStop()
onRestart()
onDestroy()
2

3
Android Activity Lifecycle Methods
Method Description Kill After Next
This is called on initial creation of the activity. It is responsible for
onCreate() constructing views, binding data to controls, and managing or restoring No onStart()
state from its given bundle.
onStart() This method is called immediately before the activity shows onscreen. No onResume()
The onResume() method fires when the activity has been created, started,
onResume() and is ready to receive user input. The activity will start interacting with No onPause()
the user after this method completes.
This method is triggered whenever the activity is visible but not
onStop()
interactive. It can be called when the system prepares to transition to
onPause() Yes Or
another activity, or it can be called while the current activity is interrupted
onResume()
and sent to the background (ex. popup is displayed).
onRestart()
onStop() This method is called when the activity is not visible. Yes Or
onDestroy()
This method is invoked after the activity has been stopped, right before
onRestart() starting up again. This happens in cases such as resuming after a phone call No onStart()
or bringing the app back to the foreground.
The method is called before the activity is destroyed. It is usually the result
onDestroy() of an explicit call to finish() from within the activity, or when the Back Yes N/A
button is pressed. This method is called at most once for the activity.
4

Cases for Activity Lifecycle


When app is first created When screen is partially obscured
calling onCreate (popup message/window appeared)
calling onStart calling onPause
calling onResume When the popup message is closed
calling onResume
When screen sleeps, or sending
app to the background When app is closed or pressing the
(another app completely Back button
obscures it) calling onPause
calling onPause calling onStop
calling onStop calling onDestroy
And again when it wakes up
calling onRestart
calling onStart
calling onResume

5
Cases for Activity Lifecycle
When Next Activity is called from Main Activity
D/MainActivity: calling Next Activity
D/MainActivity: calling onPause from MainActivity
D/NextActivity: calling onCreate from Next Activity
D/NextActivity: calling onStart from Next Activity
D/NextActivity: calling onResume from Next Activity
D/MainActivity: calling onStop from MainActivity

When Returning back to the Main Activity from Next Activity using back button
D/NextActivity: calling onPause from Next Activity
D/MainActivity: calling onRestart from MainActivity
D/MainActivity: calling onStart from MainActivity
D/MainActivity: calling onResume from MainActivity
D/NextActivity: calling onStop from Next Activity
D/NextActivity: calling onDestroy from Next Activity

Cases for Activity Lifecycle

When the app is killed in the background(to free resources


for another foreground app), onStop will be the last to be
called before the app is terminated, but it remains in the
history. When the user selects the screen from the history,
the screen starts again from onCreate() method.
onCreate and onDestroy will be called at most once each
time the application is run. But the onPause, onStop,
onRestart, onStart, onResume maybe called many times
during the lifecycle.

You might also like