티스토리 뷰

Android/Fragment

[ Fragment #1 ] Fragment Lifecycle

녹색꼬맹이 2022. 9. 19. 15:02
반응형
SMALL

Fragment는 Activity Lifecycle 내에서 또 다른 Lifecycle을 갖는 Layout으로 FragmentManager를 이용하여 관리된다.

 

방법은 replace 하는 방법과 

모두 add 시키고 show / hide 하는 방법으로 

Fragment를 관리할 수 있다.

 

이에 따른 Fragment Lifecycle이 달라질 수 있다는 것을 꼭 확인하자.

 

supportFragmentManager
            .beginTransaction()
            .replace(R.id.frameLayout, fragment)
            .commitNow()

 

----------------------------------- 최초 등록 -----------------------------------

2022-09-19 14:45:40.735 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onAttach
2022-09-19 14:45:40.735 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onCreate
2022-09-19 14:45:40.738 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onCreateView
2022-09-19 14:45:40.741 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onViewCreated
2022-09-19 14:45:40.750 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onStart
2022-09-19 14:45:40.752 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onResume

------------------------------------ replace 시점 ------------------------------------------------
2022-09-19 14:45:51.042 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onPause
2022-09-19 14:45:51.043 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onStop
2022-09-19 14:45:51.043 28922-28922/com.jihunyun.tracktest D/####: SecondFragment - onAttach
2022-09-19 14:45:51.044 28922-28922/com.jihunyun.tracktest D/####: SecondFragment - onCreate
2022-09-19 14:45:51.044 28922-28922/com.jihunyun.tracktest D/####: SecondFragment - onCreateView
2022-09-19 14:45:51.050 28922-28922/com.jihunyun.tracktest D/####: SecondFragment - onViewCreated
2022-09-19 14:45:51.051 28922-28922/com.jihunyun.tracktest D/####: SecondFragment - onStart
2022-09-19 14:45:51.055 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onDestroyView
2022-09-19 14:45:51.061 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onDestroy
2022-09-19 14:45:51.063 28922-28922/com.jihunyun.tracktest D/####: FirstFragment - onDetach
2022-09-19 14:45:51.063 28922-28922/com.jihunyun.tracktest D/####: SecondFragment - onResume

 

 

아예 Fragment가 onDetach까지 되어 날아가는 모습

 

supportFragmentManager.beginTransaction()
            .add(R.id.frameLayout, firstFragment)
            .add(R.id.frameLayout, secondFragment)
            .show(firstFragment)
            .hide(secondFragment)
            .commitNow()

 

----------------------------------- 최초 등록 -----------------------------------

2022-09-19 14:52:55.527 29134-29134/com.jihunyun.tracktest D/####: FirstFragment - onAttach
2022-09-19 14:52:55.528 29134-29134/com.jihunyun.tracktest D/####: FirstFragment - onCreate
2022-09-19 14:52:55.530 29134-29134/com.jihunyun.tracktest D/####: SecondFragment - onAttach
2022-09-19 14:52:55.530 29134-29134/com.jihunyun.tracktest D/####: SecondFragment - onCreate
2022-09-19 14:52:55.538 29134-29134/com.jihunyun.tracktest D/####: FirstFragment - onCreateView
2022-09-19 14:52:55.543 29134-29134/com.jihunyun.tracktest D/####: FirstFragment - onViewCreated
2022-09-19 14:52:55.553 29134-29134/com.jihunyun.tracktest D/####: SecondFragment - onCreateView
2022-09-19 14:52:55.555 29134-29134/com.jihunyun.tracktest D/####: SecondFragment - onViewCreated
2022-09-19 14:52:55.557 29134-29134/com.jihunyun.tracktest D/####: FirstFragment - onStart
2022-09-19 14:52:55.558 29134-29134/com.jihunyun.tracktest D/####: SecondFragment - onStart
2022-09-19 14:52:55.563 29134-29134/com.jihunyun.tracktest D/####: FirstFragment - onResume
2022-09-19 14:52:55.564 29134-29134/com.jihunyun.tracktest D/####: SecondFragment - onResume

 

모든. Fragment 가 onResume까지 진행된다. 

 

그래서 show / hide를 할 때 등록된 Fragment를 바꿔치기해도 이미 Fragment는 onResume까지 살아 있는 상태로 화면만 바뀜.

 

 

supportFragmentManager는 

commit()

commitAllowingStateLoss()

commitNow()

commitNowAllowingStateLoss()

가 있는데 

 

commit()을 권장하지만, commit과 commitNow의 차이점은 

commit 은 비동기적으로 트랜잭션에 추가하고 

commitNow는 즉시 추가된다. 

반응형
LIST

'Android > Fragment' 카테고리의 다른 글

[ Fragment #2 ] Fragment Bundle ( inc. Serializable )  (0) 2024.02.28
댓글