'Jetpack compose with two Navigation Host, hot to go back without without recompose?
Basically, I'm following the tutorial here, So here's my screen:
@Composable
fun myscreen(){
val navController = rememberNavController()
Scaffold(
bottomBar = {
BottomNavigation {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
items.forEach { screen ->
BottomNavigationItem(
icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
label = { Text(stringResource(screen.resourceId)) },
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
onClick = {
navController.navigate(screen.route) {
// Pop up to the start destination of the graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
// Restore state when reselecting a previously selected item
restoreState = true
}
}
)
}
}
}
) { innerPadding ->
NavHost(navController, startDestination = Screen.Profile.route, Modifier.padding(innerPadding)) {
composable(Screen.Profile.route) { Profile(navController) }
composable(Screen.FriendsList.route) { FriendsList(navController) }
}
}
}
This goes inside MyActivity that has navhost too
class MyActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navhost = rememberNavController()
ApplicationTheme {
Surface(color = MaterialTheme.colors.background) {
NavHost(navhost , startDestination = "myscreen") {
composable("myscreen") { myscreen() }
composable("signupscreen") { signupscreen(navhost ) }
}
}
}
}
}
}
Now if if navigate from myscreen to other different composable, when pressing back from that composable, myscreen is recomposed. but inside myscreen when i move between screens, nothing recomposed and that behavior i want when back to myscreen from other composable
Code for pressing back button from other composable
navhost.navigateUp()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
