Home > Net >  Jetpack Compose add onClick will Recomposing
Jetpack Compose add onClick will Recomposing

Time:12-28

my code

@Composable
fun AlbumPage(navHostController: NavHostController) {
   ElevatedButton(onClick = {
      navHostController.popBackStack()
   }) {
      Text(text = "click")
   }

   SideEffect {
        Log.d("tag","1111")
   }
}

when i set navHostController.popBackStack() in onClick{},log show twice

14:53:34.452 D 1111

14:53:34.816 D 1111

I remove navHostController.popBackStack()

@Composable
fun AlbumPage(navHostController: NavHostController) {
   ElevatedButton(onClick = {
      //navHostController.popBackStack()
   }) {
      Text(text = "click")
   }

   SideEffect {
        Log.d("tag","1111")
   }
}

log once

why?

CodePudding user response:

Description of the source file NavHostController.kt

When the given navController
 * changes the back stack due to a [NavController.navigate] or [NavController.popBackStack] this
 * will trigger a recompose and return the top entry on the back stack.
 *
 * @return a mutable state of the current back stack entry
  • Related