0% found this document useful (0 votes)
21 views51 pages

Activity Life Cycle

Uploaded by

Mani Xhor
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)
21 views51 pages

Activity Life Cycle

Uploaded by

Mani Xhor
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/ 51

Android Developer Fundamentals

Activities

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 1
4.0 International License
Contents
● Activity
● Activity lifecycle
● Activity lifecycle callbacks
● Activity instance state
● Saving and restoring activity state

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 2
4.0 International License
Activities
(high-level view)

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 3
4.0 International License
What is an Activity?
● An Activity is an application component
● Represents one window, one hierarchy of views
● Typically fills the screen, but can be embedded in other
activity or a appear as floating window
● Java class, typically one activity in one file

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 4
Managing State
4.0 International License
What does an Activity do?
● Represents an activity, such as ordering groceries, sending
email, or getting directions
● Handles user interactions, such as button clicks, text entry,
or login verification
● Can start other activities in the same or other apps
● Has a life cycle—is created, started, runs, is paused,
resumed, stopped, and destroyed
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Commons Attribution-NonCommercial 5
Managing State
4.0 International License
Examples of activities

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 6
Managing State
4.0 International License
Apps and activities

● Activities are loosely tied together to make up an app


● First activity user sees is typically called "main activity"
● Activities can be organized in parent-child relationships in
the Android manifest to aid navigation

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 7
Managing State
4.0 International License
Layouts and Activities

● An activity typically has a UI layout


● Layout is usually defined in one or more XML files
● Activity "inflates" layout as part of being created

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 8
Managing State
4.0 International License
Implementing
Activities

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 9
Managing State
4.0 International License
Implement new activities
1. Define layout in XML
2. Define Activity Java class
○ extends AppCompatActivity

3. Connect Activity with Layout


○ Set content view in onCreate()

4. Declare Activity in the Android manifest

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 10
Managing State
4.0 International License
1. Define layout in XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Shop for Food!" />
</RelativeLayout>
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Commons Attribution-NonCommercial 11
Managing State
4.0 International License
2. Define Activity Java class

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 12
Managing State
4.0 International License
3. Connect activity with layout

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
}
Resource is layout in this XML file
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 13
Managing State
4.0 International License
4. Declare activity in Android manifest

<activity android:name=".MainActivity">

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 14
Managing State
4.0 International License
4. Declare main activity in manifest
Main Activity needs to include intent to start from launcher icon

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 15
Managing State
4.0 International License
2.2 Activity Lifecycle and
Managing State

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 16
Managing State
4.0 International License
Activity
Lifecycle

Android Developer Fundamentals 17


What is the 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 & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 18
4.0 International License
What is the Activity Lifecycle?

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 19
4.0 International License
Activity states and app visibility
● Created (not visible yet)
● Started (visible)
● Resume (visible)
● Paused(partially invisible)
● Stopped (hidden)
● Destroyed (gone from memory)

State changes are triggered by user action, configuration


changes such as device rotation, or system action
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 20
4.0 International License
Callbacks and when they are called
onCreate(Bundle savedInstanceState)—static initialization
onStart()—when activity (screen) is becoming visible
onRestart()—called if activity was stopped (calls onStart())
onResume()—start to interact with user
onPause()—about to resume PREVIOUS activity
onStop()—no longer visible, but still exists and all state info preserved
onDestroy()—final call before Android system destroys activity

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 21
4.0 International License
Activity states and callbacks graph

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 22
4.0 International License
Implementing and overriding callbacks
● Only onCreate() is required
● Override the other callbacks to change default behavior

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 23
4.0 International License
onCreate() –> Created
● Called when the activity is first created, for example when
user taps launcher icon
● Does all static setup: create views, bind data to lists, ...
● Only called once during an activity's lifetime
● Takes a Bundle with activity's previously frozen state, if
there was one
● Created state is always followed by onStart()
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 24
4.0 International License
onCreate(Bundle savedInstanceState)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 25
4.0 International License
onStart() –> Started

● Called when the activity is becoming visible to user


● Can be called more than once during lifecycle
● Followed by onResume() if the activity comes to the
foreground, or onStop() if it becomes hidden

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 26
4.0 International License
onStart()

@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 27
4.0 International License
onRestart() –> Started

● Called after activity has been stopped, immediately before


it is started again
● Transient state
● Always followed by onStart()

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 28
4.0 International License
onRestart()

@Override
protected void onRestart() {
super.onRestart();
// The activity is between stopped and started.
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 29
4.0 International License
onResume() –> Resumed/Running

● Called when activity will start interacting with user


● Activity has moved to top of the activity stack
● Starts accepting user input
● Running state
● Always followed by onPause()

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 30
4.0 International License
onResume()

@Override
protected void onResume() {
super.onResume();
// The activity has become visible
// it is now "resumed"
}
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 31
4.0 International License
onPause() –> Paused
● 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

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 32
4.0 International License
onPause()

@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus
// this activity is about to be "paused"
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 33
4.0 International License
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
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 34
4.0 International License
onStop()

@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible
// it is now "stopped"
}
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 35
4.0 International License
onDestroy() –> Destroyed
● Final call before activity is destroyed
● User navigates back to previous activity, or configuration
changes
● Activity is finishing or system is destroying it to save space
● Call isFinishing() method to check
● System may destroy activity without calling this, so use
onPause() or onStop() to save data or state

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 36
4.0 International License
onDestroy()

@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 37
4.0 International License
Activity Instance
State

Android Developer Fundamentals 38


When does config change?
Configuration changes, invalidate the current layout or other
resources in your activity when the user:
● rotates the device
● chooses different system language, so locale changes
● enters multi-window mode (Android 7)

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 39
4.0 International License
What happens on config change?
On configuration change, Android:
1. shuts down activity 2. then starts it over
by calling: by calling:
● onPause() ● onCreate()
● onStop() ● onStart()
● onDestroy() ● onResume()

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 40
4.0 International License
Activity instance state
● State information is created while the activity is running,
such as a counter, user text, animation progression

● State is lost when device is rotated, language changes,


back-button is pressed, or the system clears memory

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 41
4.0 International License
Activity instance state
● System only saves:
○ State of views with unique ID (android:id) such as text
entered into EditText
○ Intent that started activity and data in its extras

● You are responsible for saving other activity and user


progress data

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 42
4.0 International License
Saving instance state

Implement onSaveInstanceState() in your activity

● called by Android runtime when there is a possibility the


activity may be destroyed
● saves data only for this instance of the activity during
current session

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 43
4.0 International License
onSaveInstanceState(Bundle outState)
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Add information for saving HelloToast counter
// to the outState bundle
outState.putString("count",
String.valueOf(mShowCount.getText()));
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 44
4.0 International License
Restoring instance state
Two ways to retrieve the saved Bundle
● in onCreate(Bundle mySavedState)
Preferred, to ensure that your user interface, including any
saved state, is back up and running as quickly as possible
● Implement callback (called after onStart())
onRestoreInstanceState(Bundle mySavedState)

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 45
4.0 International License
Restoring in onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowCount = (TextView) findViewById(R.id.show_count);

if (savedInstanceState != null) {
String count = savedInstanceState.getString("count");
if (mShowCount != null)
mShowCount.setText(count);
}
}
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 46
4.0 International License
onRestoreInstanceState(Bundle state)

@Override
public void onRestoreInstanceState (Bundle mySavedState) {
super.onRestoreInstanceState(mySavedState);

if (mySavedState != null) {
String count = mySavedState.getString("count");
if (count != null)
mShowCount.setText(count);
}
}

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 47
4.0 International License
Instance state and app restart

When you stop and restart a new app session, the activity
instance states are lost and your activities will revert to their
default appearance

If you need to save user data between app sessions, use


shared preferences or a database.

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Managing State
Commons Attribution-NonCommercial 48
4.0 International License
Learn more
● Activity (API Guide)
● Activity (API Reference)
● Managing the Activity Lifecycle
● Pausing and Resuming an Activity
● Stopping and Restarting an Activity
● Recreating an Activity
● Handling Runtime Changes
● Bundle
Activity Lifecycle & This work is licensed under a Creative
Android Developer Fundamentals Commons Attribution-NonCommercial 49
Managing State
4.0 International License
What's Next?

● Concept Chapter:
2.2 C Activity Lifecycle and Managing State
● Practical: 2.2 P Activity Lifecycle and Instance State

Activity Lifecycle & This work is licensed under a Creative


Android Developer Fundamentals Commons Attribution-NonCommercial 50
Managing State
4.0 International License
END

Android Developer Fundamentals 51

You might also like