Lesson 7 Activity and Fragment Lifecycles
Lesson 7 Activity and Fragment Lifecycles
Lesson 7 Activity and Fragment Lifecycles
Android Development with Kotlin v1.0 This work is licensed under the Apache 2 license. 1
About this lesson
Lesson 7: Activity and fragment lifecycles
● Activity lifecycle
● Logging
● Fragment lifecycle
● Lifecycle-aware components
● Tasks and back stack
● Summary
Android Development with Kotlin This work is licensed under the Apache 2 license. 2
Activity lifecycle
Android Development with Kotlin This work is licensed under the Apache 2 license. 3
Why it matters
Android Development with Kotlin This work is licensed under the Apache 2 license. 4
Simplified activity lifecycle
Activity launched
onCreate()
App is running
Android Development with Kotlin This work is licensed under the Apache 2 license. 5
Activity lifecycle
Activity launched onCreate()
onRestart() onStart()
onResume()
Activity running
onPause()
onStop()
Android Development with Kotlin This work is licensed under the Apache 2 license. 6
Activity states
CREATED
STARTED
RESUMED
Activity is running
PAUSED
STOPPED
DESTROYED
Android Development with Kotlin This work is licensed under the Apache 2 license. 7
onCreate()
Android Development with Kotlin This work is licensed under the Apache 2 license. 8
onStart()
Android Development with Kotlin This work is licensed under the Apache 2 license. 9
onResume()
Android Development with Kotlin This work is licensed under the Apache 2 license. 10
onPause()
Android Development with Kotlin This work is licensed under the Apache 2 license. 11
onStop()
Android Development with Kotlin This work is licensed under the Apache 2 license. 12
onDestroy()
Android Development with Kotlin This work is licensed under the Apache 2 license. 13
Summary of activity states
State Callbacks Description
Android Development with Kotlin This work is licensed under the 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.
Android Development with Kotlin This work is licensed under the Apache 2 license. 15
Logging
Android Development with Kotlin This work is licensed under the Apache 2 license. 16
Logging in Android
Android Development with Kotlin This work is licensed under the Apache 2 license. 17
Write logs
Android Development with Kotlin This work is licensed under the Apache 2 license. 18
Fragment lifecycle
Android Development with Kotlin This work is licensed under the Apache 2 license. 19
Fragment states
CREATED
STARTED
RESUMED
Fragment is running
PAUSED
STOPPED
DESTROYED
Android Development with Kotlin This work is licensed under the Apache 2 license. 20
Fragment lifecycle diagram
Fragment onViewCreated(
onAttach() onCreate() onCreateView() onStart() onResume()
is added )
Android Development with Kotlin This work is licensed under the Apache 2 license. 21
onAttach()
Android Development with Kotlin This work is licensed under the Apache 2 license. 22
onCreateView()
Android Development with Kotlin This work is licensed under the Apache 2 license. 23
onViewCreated()
Android Development with Kotlin This work is licensed under the Apache 2 license. 24
onDestroyView() and onDetach()
Android Development with Kotlin This work is licensed under the Apache 2 license. 25
Summary of fragment states
State Callbacks Description
Android Development with Kotlin This work is licensed under the Apache 2 license. 26
Save fragment state across config changes
Android Development with Kotlin This work is licensed under the Apache 2 license. 27
Lifecycle-aware
components
Android Development with Kotlin This work is licensed under the Apache 2 license. 28
Lifecycle-aware components
Android Development with Kotlin This work is licensed under the Apache 2 license. 29
LifecycleOwner
Android Development with Kotlin This work is licensed under the Apache 2 license. 30
LifecycleObserver
Implement LifecycleObserver interface:
myLifecycleOwner.getLifecycle().addObserver(MyObserver())
Android Development with Kotlin This work is licensed under the Apache 2 license. 31
Tasks and back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 32
Back stack of activities
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 33
Add to the back stack
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 34
Add to the back stack again
AttachFileActivity
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 35
Tap Back button
AttachFileActivity
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 36
Tap Back button again
ComposeActivity
EmailActivity
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 37
First destination in the back stack
First
fragment
FirstFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 38
Add a destination to the back stack
Second
fragment
SecondFragment
FirstFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 39
Tap Back button
SecondFragment
First
fragment popped off the stack
FirstFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 40
Another back stack example
ResultFragment
Question3Fragment
Result Question2Fragment
fragment
Question1Fragment
WelcomeFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 41
Modify Back button behavior
ResultFragment
WelcomeFragment
Back stack
Android Development with Kotlin This work is licensed under the Apache 2 license. 42
Summary
Android Development with Kotlin This work is licensed under the 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 additions
● 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
Android Development with Kotlin This work is licensed under the Apache 2 license. 44
Learn more
Android Development with Kotlin This work is licensed under the Apache 2 license. 45
Pathway
Android Development with Kotlin This work is licensed under the Apache 2 license. 46