Skip to content

Commit 4216769

Browse files
committed
Add note on creating new ViewModel instances
1 parent e93741c commit 4216769

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

app/src/main/java/com/example/nav3recipes/passingarguments/injectedviewmodels/InjectedViewModelsActivity.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ class InjectedViewModelsActivity : ComponentActivity() {
8282
}
8383
entry<RouteB> { key ->
8484
val viewModel = hiltViewModel<RouteBViewModel, RouteBViewModel.Factory>(
85+
// Note: We need a new ViewModel for every new RouteB instance. Usually
86+
// we would need to supply a `key` String that is unique to the
87+
// instance, however, the ViewModelStoreNavEntryDecorator (supplied
88+
// above) does this for us, using `NavEntry.contentKey` to uniquely
89+
// identify the viewModel.
90+
//
91+
// tl;dr: Make sure you use rememberViewModelStoreNavEntryDecorator()
92+
// if you want a new ViewModel for each new navigation key instance.
8593
creationCallback = { factory ->
8694
println("Creating viewModel for $key")
8795
factory.create(key)

0 commit comments

Comments
 (0)