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

Lesson 7 - Activity and Fragment Lifecycles

You should probably get to work!

Uploaded by

carlgray8719
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 views46 pages

Lesson 7 - Activity and Fragment Lifecycles

You should probably get to work!

Uploaded by

carlgray8719
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/ 46

Lesson 7:

Activity and
fragment lifecycles

This work is licensed under the


Android Development with Kotlin Apache 2 license. 1
v1.0
About this lesson
Lesson 7: Activity and fragment lifecycles
● Activity lifecycle
● Logging
● Fragment lifecycle
● Lifecycle-aware components
● Tasks and back stack
● Summary

This work is licensed under the


Android Development with Kotlin Apache 2 license. 2
Activity lifecycle

This work is licensed under the


Android Development with Kotlin Apache 2 license. 3
Why it matters

● Preserve user data and state if:


○ User temporarily leaves app and then returns
○ User is interrupted (for example, a phone call)
○ User rotates device
● Avoid memory leaks and app crashes.

This work is licensed under the


Android Development with Kotlin Apache 2 license. 4
Simplified activity lifecycle
Activity
launched

onCreate()

App is running

Activity shut
down

This work is licensed under the


Android Development with Kotlin Apache 2 license. 5
Activity lifecycle
Activity onCreate(
launched )

onRestart() onStart()

onResume(
)
Activity
running

onPause()

onStop()

onDestroy( Activity shut


) down

This work is licensed under the


Android Development with Kotlin Apache 2 license. 6
Activity states
CREATED

STARTED
RESUME
D
Activity is
running
PAUSED

STOPPED
DESTROY
ED
This work is licensed under the
Android Development with Kotlin Apache 2 license. 7
onCreate()

● Activity is created and other initialization work occurs


● You must implement this callback
● Inflate activity UI and perform other app startup logic

This work is licensed under the


Android Development with Kotlin Apache 2 license. 8
onStart()

● Activity becomes visible to the user


● Called after activity:
○ onCreate()
or
○ onRestart() if activity was previously stopped

This work is licensed under the


Android Development with Kotlin Apache 2 license. 9
onResume()

● Activity gains input focus:


○ User can interact with the activity
● Activity stays in resumed state until system triggers
activity to be paused

This work is licensed under the


Android Development with Kotlin Apache 2 license. 10
onPause()

● Activity has lost focus (not in foreground)


● Activity is still visible, but user is not actively
interacting with it
● Counterpart to onResume()

This work is licensed under the


Android Development with Kotlin Apache 2 license. 11
onStop()

● Activity is no longer visible to the user


● Release resources that aren’t needed anymore
● Save any persistent state that the user is in the process
of editing so they don’t lose their work

This work is licensed under the


Android Development with Kotlin Apache 2 license. 12
onDestroy()

● Activity is about to be destroyed, which can be caused


by:
○ Activity has finished or been dismissed by the user
○ Configuration change
● Perform any final cleanup of resources.
● Don’t rely on this method to save user data (do that
earlier)
This work is licensed under the
Android Development with Kotlin Apache 2 license. 13
Summary of activity states
State Callbacks Description

Created onCreate() Activity is being initialized.

Started onStart() Activity is visible to the user.

Resumed onResume() Activity has input focus.

Paused onPause() Activity does not have input focus.

Stopped onStop() Activity is no longer visible.

Destroyed onDestroy() Activity is destroyed.

This work is licensed under the


Android Development with Kotlin Apache 2 license. 14
Save state
User expects UI state to stay the same after a config change or if
the app is terminated when in the background.
● Activity is destroyed and restarted, or app is terminated and
activity is started.
● Store user data needed to reconstruct app and activity
Lifecycle changes:
○ Use Bundle provided by onSaveInstanceState().
○ onCreate() receives the Bundle as an argument when
activity is created again.

This work is licensed under the


Android Development with Kotlin Apache 2 license. 15
Logging

This work is licensed under the


Android Development with Kotlin Apache 2 license. 16
Logging in Android
● Monitor the flow of events or state of your app.
● Use the built-in Log class or third-party library.
● Example Log method call: Log.d(TAG, "Message")

This work is licensed under the


Android Development with Kotlin Apache 2 license. 17
Write logs

Priority level Log method

Verbose Log.v(String, String)


Debug Log.d(String, String)
Info Log.i(String, String)
Warning Log.w(String, String)
Error Log.e(String, String)

This work is licensed under the


Android Development with Kotlin Apache 2 license. 18
Fragment lifecycle

This work is licensed under the


Android Development with Kotlin Apache 2 license. 19
Fragment states
CREATED

STARTED
RESUME
D
Fragment is
running
PAUSED

STOPPED
DESTROY
ED
This work is licensed under the
Android Development with Kotlin Apache 2 license. 20
Fragment lifecycle diagram

Fragmen
onAttach( onCreate( onCreateView( onViewCreated onStart( onResume(
t is ) ) ) () ) )
added

Fragmen Fragment
onPause( onDestroyView onDestroy( onDetach(
t is
)
onStop()
() ) ) is
active destroyed

This work is licensed under the


Android Development with Kotlin Apache 2 license. 21
onAttach()

● Called when a fragment is attached to a context


● Immediately precedes onCreate()

This work is licensed under the


Android Development with Kotlin Apache 2 license. 22
onCreateView()

● Called to create the view hierarchy associated with the


fragment
● Inflate the fragment layout here and return the root
view

This work is licensed under the


Android Development with Kotlin Apache 2 license. 23
onViewCreated()

● Called when view hierarchy has already been created


● Perform any remaining initialization here (for example,
restore state from Bundle)

This work is licensed under the


Android Development with Kotlin Apache 2 license. 24
onDestroyView() and
onDetach()

● onDestroyView() is called when view hierarchy of


fragment is removed.
● onDetach() is called when fragment is no longer
attached to the host.

This work is licensed under the


Android Development with Kotlin Apache 2 license. 25
Summary of fragment states
State Callbacks Description
Initialized onAttach() Fragment is attached to host.

Created onCreate(), onCreateView(), Fragment is created and layout is being


onViewCreated() initialized.
Started onStart() Fragment is started and visible.

Resumed onResume() Fragment has input focus.

Paused onPause() Fragment no longer has input focus.

Stopped onStop() Fragment is not visible.

Destroyed onDestroyView(), onDestroy(), Fragment is removed from host.


onDetach()

This work is licensed under the


Android Development with Kotlin Apache 2 license. 26
Save fragment state across config
changes
Preserve UI state in fragments by storing state in Bundle:
● onSaveInstanceState(outState: Bundle)

Retrieve that data by receiving the Bundle in these


fragment callbacks:
● onCreate()
● onCreateView()
● onViewCreated()
This work is licensed under the
Android Development with Kotlin Apache 2 license. 27
Lifecycle-aware
components

This work is licensed under the


Android Development with Kotlin Apache 2 license. 28
Lifecycle-aware components

Adjust their behavior based on activity or fragment


lifecycle
● Use the androidx.lifecycle library
● Lifecycle tracks the lifecycle state of an activity or
fragment
○ Holds current lifecycle state
○ Dispatches lifecycle events (when there are state
changes)
Android Development with Kotlin
This work is licensed under the
Apache 2 license. 29
LifecycleOwner

● Interface that says this class has a lifecycle


● Implementers must implement getLifecycle()
method
Examples: Fragment and AppCompatActivity are
implementations of LifecycleOwner

This work is licensed under the


Android Development with Kotlin Apache 2 license. 30
LifecycleObserver
Implement LifecycleObserver interface:

class MyObserver : LifecycleObserver {


@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun connectListener() {
...
}

Add the observer to the lifecycle:

myLifecycleOwner.getLifecycle().addObserver(MyObserver())

This work is licensed under the


Android Development with Kotlin Apache 2 license. 31
Tasks and back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 32
Back stack of activities

EmailActivity

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 33
Add to the back stack

ComposeActivity

EmailActivity

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 34
Add to the back stack again

AttachFileActivity

ComposeActivity

EmailActivity

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 35
Tap Back button

AttachFileActivity

popped off the stack

ComposeActivity

EmailActivity

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 36
Tap Back button again

ComposeActivity

popped off the stack

EmailActivity

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 37
First destination in the back stack

First
fragment

FirstFragment

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 38
Add a destination to the back
stack

Second
fragment SecondFragme
nt

FirstFragment

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 39
Tap Back button

SecondFragme
nt
First
fragment popped off the stack

FirstFragment

Back stack

This work is licensed under the


Android Development with Kotlin Apache 2 license. 40
Another back stack example
ResultFragment

Question3Fragment

Result Question2Fragment
fragment
Question1Fragment

WelcomeFragment

Back stack
This work is licensed under the
Android Development with Kotlin Apache 2 license. 41
Modify Back button behavior
ResultFragment

pop additional destinations Question3Fragment


off the back stack
Welcome Question2Fragment
fragment
Question1Fragment

WelcomeFragment

Back stack
This work is licensed under the
Android Development with Kotlin Apache 2 license. 42
Summary

This work is licensed under the


Android Development with Kotlin Apache 2 license. 43
Summary
In Lesson 7, you learned how to:
● Understand how an activity instance transitions through different
lifecycle states as the user interacts with or leaves your app
● Reserve UI state across configuration changes using a Bundle
● Fragment lifecycle callback methods similar to activity, but with a
dditions
● Use lifecycle-aware components help organize your app code
● Use default or custom back stack behavior
● Use logging to help debug and track the state of the app

This work is licensed under the


Android Development with Kotlin Apache 2 license. 44
Learn more

● Understand the Activity Lifecycle


● Activity class
● Fragments guide and lifecycle
● Fragment class

This work is licensed under the


Android Development with Kotlin Apache 2 license. 45
Pathway

Practice what you’ve learned by


completing the pathway:
Lesson 7: Activity and FragmentLifecycles

This work is licensed under the


Android Development with Kotlin Apache 2 license. 46

You might also like