[Android, Kotlin] Fragment Lifecycle 상황별 동작 순서 (add, replace, back stack)
Android

[Android, Kotlin] Fragment Lifecycle 상황별 동작 순서 (add, replace, back stack)

(1) Activity 및 FirstFragment 최초 생성 및 attach

 

add, add(backstack)

 

 

 

replace, replace(backstack)

Activity, Fragment 최초 생성의 경우 모두 같은 순서를 가짐.

 

 

 

 

(2) SecondFragment 생성

 

add, add(backstack)

[ Add, Add(backstack) ]

기존 FirstFragment의 변화 없이 SecondFragment 생성됨

 

 

 

replace

[ Replace ]

First Fragment onPause -> onStop

Second Fragment onAttach ~ onStart

First Fragment onDestoyView ~ onDetach

Second Fragment onResume

 

 

 

replace (backstack)

[ Replace(backstack) ]

First Fragment onPause -> onStop

Second Fragment onAttach ~ onStart

First Fragment onDestoyView

Second Fragment onResume

 

* 기존 replace와 달리 FirstFragment는 backstack으로 남아있으므로 view까지만 소멸됨. Fragment 자체는 소멸 안됨.

 

 

 

 

 

(3) Home 버튼 클릭

 

add, add(backstack)

[ Add, Add(backstack) ]

First, Second Fragment, Activity onPause

First, Second Fragment onSaveInstanceState

First, Second Fragment, Activity onStop

 

 

 

 

replace

[ Replace ]

Second Fragment, MainActivity onPause

Second Fragment onSaveInstanceState

Second Fragment, MainActivity onStop

 

 

 

 

replace (backstack)

[ Replace(backstack) ]

Second Fragment, MainActivity onPause

First, Second Fragment onSaveInstanceState

Second Fragment, MainActivity onStop

 

* FirstFragment의 경우 아직 Fragment가 소멸된 상태가 아님. 다시 View가 Create 될 수 있으므로 SaveInstanceState 메소드 수행됨

 

 

 

 

 

(4)  다시 앱으로 돌아옴

 

add, add(backstack)

[ Add, Add(backstack) ]

First, Second Fragment, MainActivity onStart -> onResume

 

 

 

 

replace, replace(backstack)

[ Replace, Replace(backstack) ]

Second Fragment, MainActivity onStart -> onResume

 

 

 

 

(5) back 버튼

 

add

[ Add ]

First, Second Fragment, MainActivity onPause -> onStop

FirstFragment, SecondFragment onDestroyView ~ onDetach

MainActivity onDestroy

 

 

 

 

add (backstack)

[ Add(backstack) ]

Second Fragment onPause ~ onDetach

 

* add - backstack에 의해 기존 FirstFragment는 View가 소멸되지 않고 그대로 있었으므로 SecondFragment만 소멸

 

 

 

 

 

replace

[Replace]

Second Fragment, MainActivity onPause -> onStop

SecondFragment onDestroyView ~ onDetach

MainActivity onDestroy

 

 

 

 

replace (backstack)

[Replace]

Second Fragment onPause -> onStop

First Fragment onCreateView ~ onStart

SecondFragment onDestroyView ~ onDetach

First Fragment onResume

 

* backstack에 의해 view만 소멸되어있는 상태였던 FirstFragment가 다시 top-level에 올라옴으로써 view 재생성됨

 

 

 

 

 

(6) back 버튼 한 번 더 (back stack 경우)

 

add (backstack), replace (backstack)

[ Add (backstack), Replace (backstack) ]

First Fragment, MainActivity onPause -> onStop

First Fragment onDestroyView ~ onDetach

MainActivity onDestroy

 

 

 

 

Github 소스코드 : https://github.com/HunSeongPark/Kotlin-App-Study/tree/main/fragment-lifecycle 

 

GitHub - HunSeongPark/Kotlin-App-Study: Kotlin App 실습 관련

Kotlin App 실습 관련. Contribute to HunSeongPark/Kotlin-App-Study development by creating an account on GitHub.

github.com