LoginSignup
0
1

More than 1 year has passed since last update.

[Android][Jetpack Compose]You cannot access the NavBackStackEntry's ViewModels after the NavBackStackEntry is destroyed.

Last updated at Posted at 2023-01-30

NavControllerのpopBackStackを使って前画面に戻るという実装をしたが
実行時にエラーが起きてアプリが落ちた。

FATAL EXCEPTION: main
Process: ........, PID: 6785
java.lang.IllegalStateException: You cannot access the NavBackStackEntry's ViewModels after the NavBackStackEntry is destroyed.
at androidx.navigation.NavBackStackEntry.getViewModelStore(NavBackStackEntry.kt:202)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:104)
at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:67)
.
.
.
省略

解決方法

popBackStack()ではなくて

navigateUp()

を使う

参考

navigateUp
ナビゲーション階層で上に移動することを試みる。
ユーザーが、アプリ UI の左上(または開始)隅にある左向きの矢印でマークされた「Up」ボタンを押したときに適しています。

popBackStack
コントローラのバックスタックをポップしようとします。
これは、ナビゲーションホストにフォーカスがあるときに、ユーザがシステムの戻るボタンを押したときに似ています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1