티스토리 뷰
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는 즉시 추가된다.
'Android > Fragment' 카테고리의 다른 글
[ Fragment #2 ] Fragment Bundle ( inc. Serializable ) (0) | 2024.02.28 |
---|
- Total
- Today
- Yesterday
- flutter web
- 랜덤컬러
- 프래그먼트 데이터 저장
- android widget 만들기
- 쏘렌토 하이브리드 시그니처
- Android
- RandomColor
- widget 이미지 로딩
- Dot indicator
- 풀배열 키보드
- 안드로이드 플립 대응
- 가중치랜덤
- 맥 전용
- 폰과 워치 페어링
- 폴더블 이벤트 받기
- widget onreceive
- widget 만들기
- Foldable Event
- 쏘렌토 하이브리드
- 폴더블 상태 유지
- 웨어러블
- 갤럭시 워치
- Fragment data
- widget onupdate
- aos img url bitmap
- widget glide
- Android Wear Os
- 역시 키크론이네
- wear os
- 안드로이드 폴드 대응
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |