Skip to content

Commit a6f3406

Browse files
authored
Merge pull request #38 from android/dt/check-vm-factory
Add comment about creating new ViewModel instances
2 parents ef45ff2 + 7c6c49e commit a6f3406

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

app/src/main/java/com/example/nav3recipes/passingarguments/basicviewmodels/BasicViewModelsActivity.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.example.nav3recipes.passingarguments.basicviewmodels
1919
import android.os.Bundle
2020
import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
22+
import androidx.compose.foundation.lazy.LazyColumn
2223
import androidx.compose.material3.Button
2324
import androidx.compose.material3.Text
2425
import androidx.compose.runtime.Composable
@@ -57,6 +58,10 @@ class BasicViewModelsActivity : ComponentActivity() {
5758
NavDisplay(
5859
backStack = backStack,
5960
onBack = { backStack.removeLastOrNull() },
61+
// In order to add the `ViewModelStoreNavEntryDecorator` (see comment below for why)
62+
// we also need to add the default `NavEntryDecorator`s as well. These provide
63+
// extra information to the entry's content to enable it to display correctly
64+
// and save its state.
6065
entryDecorators = listOf(
6166
rememberSceneSetupNavEntryDecorator(),
6267
rememberSavedStateNavEntryDecorator(),
@@ -65,16 +70,26 @@ class BasicViewModelsActivity : ComponentActivity() {
6570
entryProvider = entryProvider {
6671
entry<RouteA> {
6772
ContentGreen("Welcome to Nav3") {
68-
Button(onClick = {
69-
backStack.add(
70-
RouteB("123")
71-
)
72-
}) {
73-
Text("Click to navigate")
73+
LazyColumn {
74+
items(10) { i ->
75+
Button(onClick = {
76+
backStack.add(RouteB("$i"))
77+
}) {
78+
Text("$i")
79+
}
80+
}
7481
}
7582
}
7683
}
7784
entry<RouteB> { key ->
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.
7893
ScreenB(viewModel = viewModel(factory = RouteBViewModel.Factory(key)))
7994
}
8095
}

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package com.example.nav3recipes.passingarguments.injectedviewmodels
1919
import android.os.Bundle
2020
import androidx.activity.ComponentActivity
2121
import androidx.activity.compose.setContent
22+
import androidx.compose.foundation.lazy.LazyColumn
2223
import androidx.compose.material3.Button
2324
import androidx.compose.material3.Text
2425
import androidx.compose.runtime.Composable
@@ -34,6 +35,7 @@ import androidx.navigation3.ui.NavDisplay
3435
import androidx.navigation3.ui.rememberSceneSetupNavEntryDecorator
3536
import com.example.nav3recipes.content.ContentBlue
3637
import com.example.nav3recipes.content.ContentGreen
38+
import com.example.nav3recipes.passingarguments.basicviewmodels.RouteB
3739
import com.example.nav3recipes.ui.setEdgeToEdgeConfig
3840
import dagger.assisted.Assisted
3941
import dagger.assisted.AssistedFactory
@@ -63,6 +65,11 @@ class InjectedViewModelsActivity : ComponentActivity() {
6365
NavDisplay(
6466
backStack = backStack,
6567
onBack = { backStack.removeLastOrNull() },
68+
69+
// In order to add the `ViewModelStoreNavEntryDecorator` (see comment below for why)
70+
// we also need to add the default `NavEntryDecorator`s as well. These provide
71+
// extra information to the entry's content to enable it to display correctly
72+
// and save its state.
6673
entryDecorators = listOf(
6774
rememberSceneSetupNavEntryDecorator(),
6875
rememberSavedStateNavEntryDecorator(),
@@ -71,15 +78,27 @@ class InjectedViewModelsActivity : ComponentActivity() {
7178
entryProvider = entryProvider {
7279
entry<RouteA> {
7380
ContentGreen("Welcome to Nav3") {
74-
Button(onClick = {
75-
backStack.add(RouteB("123"))
76-
}) {
77-
Text("Click to navigate")
81+
LazyColumn {
82+
items(10) { i ->
83+
Button(onClick = {
84+
backStack.add(RouteB("$i"))
85+
}) {
86+
Text("$i")
87+
}
88+
}
7889
}
7990
}
8091
}
8192
entry<RouteB> { key ->
8293
val viewModel = hiltViewModel<RouteBViewModel, RouteBViewModel.Factory>(
94+
// Note: We need a new ViewModel for every new RouteB instance. Usually
95+
// we would need to supply a `key` String that is unique to the
96+
// instance, however, the ViewModelStoreNavEntryDecorator (supplied
97+
// above) does this for us, using `NavEntry.contentKey` to uniquely
98+
// identify the viewModel.
99+
//
100+
// tl;dr: Make sure you use rememberViewModelStoreNavEntryDecorator()
101+
// if you want a new ViewModel for each new navigation key instance.
83102
creationCallback = { factory ->
84103
factory.create(key)
85104
}

0 commit comments

Comments
 (0)