Lesson 7 - Activity and Fragment Lifecycles
Lesson 7 - Activity and Fragment Lifecycles
Activity and
fragment lifecycles
onCreate()
App is running
Activity shut
down
onRestart() onStart()
onResume(
)
Activity
running
onPause()
onStop()
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()
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
myLifecycleOwner.getLifecycle().addObserver(MyObserver())
EmailActivity
Back stack
ComposeActivity
EmailActivity
Back stack
AttachFileActivity
ComposeActivity
EmailActivity
Back stack
AttachFileActivity
ComposeActivity
EmailActivity
Back stack
ComposeActivity
EmailActivity
Back stack
First
fragment
FirstFragment
Back stack
Second
fragment SecondFragme
nt
FirstFragment
Back stack
SecondFragme
nt
First
fragment popped off the stack
FirstFragment
Back stack
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
WelcomeFragment
Back stack
This work is licensed under the
Android Development with Kotlin Apache 2 license. 42
Summary