ursus
03/30/2026, 10:35 PMnav3 when I push say data object Profile : NavKey, once
I get the matching profile screen & it's viewmodel displayed as expected (and in the viewmodel I have a timer ticking every second and incrementing counter)
When I push Profile again on top of it, so backstack is [Profile, Profile] I oberve the top screen is the first profile, not the latest profile (juding by the counter)
is this a bug in nav3?Aditya Bhaskar
03/30/2026, 10:44 PMkey to something like hiltViewModel if you need more than one insurance of the same view model for different/repeated destinations.ursus
03/30/2026, 10:45 PMursus
03/30/2026, 10:46 PMRey (Kingg22)
03/30/2026, 11:06 PMursus
03/30/2026, 11:08 PM@Serializable
data object Profile : NavKey
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
val backStack = rememberNavBackStack(Profile)
NavDisplay(
backStack = backStack,
onBack = { backStack.removeLastOrNull() },
entryProvider = entryProvider {
entry<Profile> {
val depth = backStack.count { it == Profile }
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Hello world")
Text(text = "Profile count on stack: $depth")
Button(
onClick = dropUnlessResumed {
backStack.add(Profile)
}
) {
Text(text = "Push Profile again")
}
}
}
}
)
}
}
}
}Ian Lake
03/30/2026, 11:08 PMcontentKey - that is what the state of the screen is tied to. If your keys are data class Profile(id: String), then you'll get a different contentKey for each unique id and unique state for each instance of a given id. If you use a data object, then you'll get the exact same contentKey for every position that key is on the back stack, so you should expect them all to share the same state, that is working as intendedursus
03/30/2026, 11:12 PM[a, b, a, b] to get only 2 viewmodels in memoryIan Lake
03/30/2026, 11:15 PMdata object to model something you'd put multiple times on the back stack in the first place, but maybe I'm missing something on how you got yourself into this in the first place?ursus
03/30/2026, 11:16 PMclass Profile : NavKey(), right?Ian Lake
03/30/2026, 11:19 PMcontentKey, so you'd need to add something in there that is unique to what profile you want to display - the default contentKey of a NavEntry is just key.toString() which is great for a data class , but not useful if you don't have anything different in each instanceIan Lake
03/30/2026, 11:19 PMursus
03/30/2026, 11:20 PMclass and therefore work as well? generating random uuids seems like a overkillIan Lake
03/30/2026, 11:20 PMdata class 's toString() just outputs all of the properties of the classursus
03/30/2026, 11:21 PMclass ProfileIan Lake
03/30/2026, 11:21 PMIan Lake
03/30/2026, 11:22 PMcontentKey of 0x12341234 but after a process death you get a new contentKey of 0x56785678 == no state restoredursus
03/30/2026, 11:23 PM[A("123"), B("321"), A("123")] would yield the same effectIan Lake
03/30/2026, 11:25 PMIan Lake
03/30/2026, 11:26 PMProfile("123") to be different from Profile("123"), you're overthinking things)ursus
03/30/2026, 11:29 PMdata object) are fine, doesn't it?Ian Lake
03/30/2026, 11:30 PMdata object screen for things like your Home screen or a Login screen, absolutelyursus
03/30/2026, 11:31 PMI'm not sure why you'd use aso then I probably misunderstood thisto model something you'd put multiple times on the back stack in the first place,data object
ursus
03/30/2026, 11:31 PM