简体   繁体   中英

Jetpack Compose add onClick will Recomposing

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM