0% found this document useful (0 votes)
57 views17 pages

Activity Lifecycle & Callbacks

The document discusses the activity lifecycle and callbacks in Android. It describes the different states an activity can be in, from created to destroyed, and the associated callbacks for each state transition. The key states are created, started, resumed, paused, stopped, and destroyed. The main callbacks that can be overridden are onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). Implementing these callbacks properly ensures activities handle configuration changes, release resources appropriately, and save user progress. The activity lifecycle is an important concept in Android development for controlling activity behavior during state changes.

Uploaded by

Ryan Ali Pratama
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)
57 views17 pages

Activity Lifecycle & Callbacks

The document discusses the activity lifecycle and callbacks in Android. It describes the different states an activity can be in, from created to destroyed, and the associated callbacks for each state transition. The key states are created, started, resumed, paused, stopped, and destroyed. The main callbacks that can be overridden are onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). Implementing these callbacks properly ensures activities handle configuration changes, release resources appropriately, and save user progress. The activity lifecycle is an important concept in Android development for controlling activity behavior during state changes.

Uploaded by

Ryan Ali Pratama
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/ 17

Activity Lifecycle & Callbacks

o Activity Lifecycle
Contents o Activity Lifecycle Callbacks
Activity Lifecycle

● The set of states an activity can be in during its lifetime, from


when it is created until it is destroyed
More formally:
● A directed graph of all the states an activity can be in, and the
callbacks associated with transitioning from each state to the
next one
Activity Lifecycle
Why Use Activity Lifecycle Callback Method?

If you do a good implementation of Lifecycle call-backs methods in your android app it


will avoid many problems, Like :

1. If the user files the form and suddenly rotate the screen so all filled data will be lost,
It’s not good.

2. Android limited resources can crash your app if you didn’t release on time

3. Losing the user’s progress if they leave your app and return to it at a later time.
Activity States and Callbacks

● Created (not visible yet) ● onCreate()


● Started (visible) ● onStart ()
● Resume (visible) ● onResume ()
● Paused(partially invisible) ● onPause()
● Stopped (hidden) ● onStop ()
● Destroyed (gone from ● onDestroy()
memory)
Activity states change

State changes are triggered by


• user action
• configuration changes → device rotation, or system
action
Activity states and
callbacks graph
Implementing and overriding callbacks

● Only onCreate() is required


● Override the other callbacks to change default
behavior
onCreate() → Created

● Implement this callback when the system first


creates the activity to enters the Creates states
● Only called once for entire life of the activity, such
as bind data to lists, initialize background threads,
and instantiate some class-scope variables
● Receives the parameter savedInstanceState, which
is a Bundle object
● shows fundamental setup for the activity, such as
declaring the user interface (defined in an XML
layout file), defining member variables, and
configuring some of the UI.
onStart() → Started

● The onStart() call makes the activity visible to


the user, as the app prepares for the activity to
enter the foreground and become interactive
● Can be called more than once during lifecycle
● Followed by onResume() if the activity comes to
the foreground, or onStop() if it becomes
hidden
onResume() → Resumed/Running

● Called when the activity enters the Resumed


state, or comes to the foreground
● The state which the app interact with user,
and stays in this state until something
happens to take focus away from the app.
Such as, receiving a phone call, the user’s
navigating to another activity, or the device
screen’s turning off.
● Always followed by
onPause() → Paused

● The system calls this method as the first indication that the user is
leaving your activity (though it does not always mean the activity is being
destroyed); it indicates that the activity is no longer in the foreground
(though it may still be visible if the user is in multi-window mode).
● Called when system is about to resume a previous activity
● The activity is partly visible but user is leaving the activity
● Typically used to commit unsaved changes to persistent data, stop
animations and anything that consumes resources
● Implementations must be fast because the next activity is not resumed
until this method returns
● Followed by either onResume() if the activity returns back to the front, or
onStop() if it becomes invisible to the user
onStop() → Stopped

● Called when the activity is no longer visible to


the user
● New activity is being started, an existing one
is brought in front of this one, or this one is
being destroyed
● Operations that were too heavy-weight for
onPause
● Followed by either onRestart() if this activity
is coming back to interact with the user, or
onDestroy() if this activity is going away
onDestroy() → Destroyed

● Final call before activity is destroyed


● User navigates back to previous activity, or
configuration changes (such as device
rotation or multi-window mode)
● Activity is finishing or system is destroying
it to save space (memory issue)
● System may destroy activity without
calling this, so use onPause() or
onStop() to save data or state
Some More Callbacks Method

onSaveInstanceState(Bundle onRestoreInstanceState(Bundle
outstate) savedInstanceState)

As your activity begins to stop, the When your activity is recreated after it was

system calls the method previously destroyed, you can recover your
saved instance state from the Bundle that
onSaveInstanceState() so your
the system passes to your activity. Both the
activity can save state information
onCreate() and onRestoreInstanceState()
to an instance state bundle
callback methods receive the same Bundle
that contains the instance state
information.
Lifecycle Practical Situation

You might also like