0% found this document useful (0 votes)
25 views19 pages

Android Lifecycle

The document discusses the Android activity lifecycle. It explains that activities have different states as they are created, started, resumed, paused, stopped and destroyed. Key lifecycle methods like onCreate(), onStart(), onResume(), onPause(), onStop() and onDestroy() are called by the Android OS as the activity transitions between these states. The document provides details on what occurs in each state and examples of when different state transitions take place.

Uploaded by

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

Android Lifecycle

The document discusses the Android activity lifecycle. It explains that activities have different states as they are created, started, resumed, paused, stopped and destroyed. Key lifecycle methods like onCreate(), onStart(), onResume(), onPause(), onStop() and onDestroy() are called by the Android OS as the activity transitions between these states. The document provides details on what occurs in each state and examples of when different state transitions take place.

Uploaded by

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

Android Lifecycle

Mobile Application Development


Android Lifecycle
⚫ Android lifecycle will explore how
applications construct from the creation to
destruction.

⚫ You will learn how Android code is


structured.

⚫ We can write classes and within those classes


we have functions and these functions
contains our code, which gets things done.
Android Lifecycle
⚫ Android interacts with our applications by
calling functions that are contained
within the activity class.

⚫ Even if the function is not visible within


our calling code, It is still being called by
the Android at appropriate time.
Android Lifecycle
⚫ When we interact with the android OS,
we are not using a single application.
⚫ But we are interacting with multiple
applications at the same time.
⚫ Like you are using Facebook and you
received a whatsapp message. In this
scenario you will click on whatsapp
notification and the Facebook app will go
in the background.
Android Activity Lifecycle
⚫ Activity Lifecycle: 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.
⚫ In other words we can say Activity is a class pre-written in
Java Programming.
Activity Lifecycle - States
Name Description
onCreate() Called when the activity is first created

onStart() Called just after it’s creation or by restart method


after onStop().
Here Activity start becoming visible to user
onResume() Called when Activity is visible to user and user
can interact with it.
onPause() Called when Activity content is not visible
because user resume previous activity.
onStop() Called when activity is not visible to user
because some other activity takes place of it.
onRestart() Called when user comes on screen or resume
the activity which was stopped.
Activity Lifecycle States
⚫ Different States Activity Lifecycle
Diagram.
Activity Lifecycle
Diagram
Overriding
⚫ In any object-oriented programming language,
⚫ Overriding is a feature that allows a subclass or
child class to provide a specific implementation of
a method that is already provided by one of its
super-classes or parent classes.

⚫ The method/function in a subclass has the same


name, same parameters or signature, and same
return type(or sub-type) as a method in its super-
class.
◦ Then the method in the subclass is said to override the
method in the super-class.
Overriding onCreate()
⚫ In the android studio MainActivity.java file, we
see the code as shown below.
⚫ On line 9 creates a overridden function.
⚫ The line 11 executes the hidden onCreate()
function of parent class first.
Overriding the Lifecycle
⚫ Activity have different states or it’s
known as Activity life cycle.
⚫ The code for all the states for the lifecycle
is already implemented.
⚫ All life cycle methods/states aren’t
required to override.
◦ But it’s quite important to understand them.
⚫ Lifecycles methods can be overridden
according to user requirements.
Activity Created: onCreate()
⚫ onCreate() method is called when activity gets
memory in the OS.
◦ To use create state we need to override onCreate(Bundle
savedInstanceState) method.

⚫ Now there will be question in mind what is Bundle


here, so Bundle is a data repository object that can
store any kind of primitive data and this object will
be null until some data isn’t saved in that.

⚫ It is best place to put initialization code.


Activity Started: onStart()
⚫ onStart() method is called just after it’s creation.
⚫ In other case Activity can also be started by calling
restart method
◦ i.e after activity stop.
⚫ So this means onStart() gets called by Android OS
when user switch between applications.
◦ For example, if a user was using Application A and then a
notification comes and user clicked on notification and
moved to Application B,
◦ in this case Application A will be paused.
◦ Again if a user again click on app icon of Application A then
Application A which was stopped will again gets started.
Activity Resumed:.onResume()
⚫ Activity resumed is that situation when it
is actually visible to user.
⚫ Means the data displayed in the activity is
visible to user.
⚫ In lifecycle it always gets called after
activity start and in most use case after
activity paused (onPause).
Activity Paused: onPause()
⚫ Activity is called paused when it’s content
is not visible to user.
⚫ In most case onPause() method called
◦ By Android OS when user press Home button (Center Button
on Device) to make hide.
◦ When user press the Back navigation button.
◦ When a notification or some other dialog is overlaying any
part (top or bottom) of the activity (screen).

⚫ For example, if a call or notification comes


in, the user will get the opportunity to take
the call or ignore it.
Activity Stopped: onStop()
⚫ Activity is called stopped when it’s not visible to user.
⚫ Any activity gets stopped in case some other activity takes
place of it.
◦ For example, if a user was on screen 1 and click on some button and
moves to screen 2.
◦ In this case Activity displaying content for screen 1 will be stopped.

⚫ Every activity gets stopped before destroy in case of when user


press back navigation button.
⚫ So Activity will be in stopped state when hidden or replaced by
other activities that have been launched or switched by user.
⚫ In this case application will not present anything useful to the
user directly as it’s going to stop.
Activity Restarted: onRestart()
⚫ Activity is called in restart state after stop
state.
⚫ So activity’s onRestart() function gets called
when user comes on screen or resume the
activity which was stopped.
⚫ In other words, when Operating System
starts the activity for the first time
onRestart() never gets called.
⚫ It gets called only in case when activity is
resumes after stopped state.
Activity Destroyed: onDestroy()
⚫ Any activity is known as in destroyed state when it’s not in
background. There can different cases at what time activity get
destroyed.
◦ First is if user pressed the back navigation button then activity will be
destroyed after completing the lifecycle of pause and stop.
◦ In case if user press the home button and app moves to background.
◦ User is not using it no more and it’s being shown in recent apps list.
⚫ So in this case if system required resources need to use somewhere
else then OS can destroy the Activity.

⚫ After the Activity is destroyed if user again click the app icon, in this
case activity will be recreated and follow the same lifecycle again.
⚫ Another use case is with Splash Screens if there is call to finish()
method from onCreate() of an activity then OS can directly call
onDestroy().

You might also like