Todo ページと Poke ページで遷移します。
LocalLifecycleStateLogger()
をそれぞれに貼ります。
@Composable
fun TodoScreen(
viewModel: TodoViewModel = hiltViewModel()
) {
LocalLifecycleStateLogger(" TodoScreen") // *
@Composable
fun PokeScreen(
viewModel: TodoViewModel = hiltViewModel()
) {
LocalLifecycleStateLogger(" PokeScreen") // *
以下の順序でタップしてスタックを重ねます。
アプリ起動
↓ ① click
TodoScreen
↓ ② click
PokeScreen
↓ ③ click
TodoScreen
↓ ④ back
PokeScreen
↓ ⑤ back
TodoScreen
↓ ⑥ back
アプリ終了
出力結果。
D: @@@ MainScreen - MainActivity@a542ab9 -> ON_CREATE
D: @@@ MainScreen - MainActivity@a542ab9 -> ON_START
D: @@@ MainScreen - MainActivity@a542ab9 -> ON_RESUME
D: @@@ TodoViewModel@abae21d initialized.
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_CREATE
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_START
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_RESUME
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_PAUSE
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_STOP
D: @@@ PokeViewModel@ad0cefd initialized.
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_CREATE
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_START
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_RESUME
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_PAUSE
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_STOP
D: @@@ TodoViewModel@577b5b1 initialized.
D: @@@ TodoScreen - NavBackStackEntry@f1d51055 -> ON_CREATE
D: @@@ TodoScreen - NavBackStackEntry@f1d51055 -> ON_START
D: @@@ TodoScreen - NavBackStackEntry@f1d51055 -> ON_RESUME
D: @@@ TodoScreen - NavBackStackEntry@f1d51055 -> ON_PAUSE
D: @@@ TodoScreen - NavBackStackEntry@f1d51055 -> ON_STOP
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_CREATE
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_START
D: @@@ TodoScreen - NavBackStackEntry@f1d51055 -> ON_DESTROY
D: @@@ TodoViewModel@577b5b1 onCleared.
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_RESUME
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_PAUSE
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_STOP
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_CREATE
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_START
D: @@@ PokeScreen - NavBackStackEntry@4049e1c3 -> ON_DESTROY
D: @@@ PokeViewModel@ad0cefd onCleared.
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_RESUME
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_PAUSE
D: @@@ MainScreen - MainActivity@a542ab9 -> ON_PAUSE
D: @@@ TodoScreen - NavBackStackEntry@8ad648d7 -> ON_STOP
D: @@@ MainScreen - MainActivity@a542ab9 -> ON_STOP
ログから 各Screen = NavBackStackEntry のスタック状態を考えます。
-
↓ ① click
TodoScreen1 ↔ TodoViewModel1
MainScreen1 - MainActivity
↓ ② click
PokeScreen1 ↔ PokeViewModel1
TodoScreen1 ↔ TodoViewModel1
MainScreen1 - MainActivity
↓ ③ click
TodoScreen2 ↔ TodoViewModel2
PokeScreen1 ↔ PokeViewModel1
TodoScreen1 ↔ TodoViewModel1
MainScreen@1 - MainActivity
↓ ④ back
PokeScreen1 ↔ TodoViewModel2
TodoScreen1 ↔ TodoViewModel1
MainScreen1 - MainActivity
↓ ⑤ back
TodoScreen1 ↔ TodoViewModel1
MainScreen1 - MainActivity
↓ ⑥ back
-
意図通りに、画面がスタックされ、ライフサイクルされ、ViewModel もそれに合わせて生き死にすることが確認できます。
👉 【Jetpack Compose】 NavBackStackEntry - Composable のライフサイクルと ViewModel の状態を確認する
👉 【Kotlin】Flow の挙動やライフサイクルをログで確認する