Skip to content

Instantly share code, notes, and snippets.

@devrath
Created January 22, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devrath/fd85861a423e37d18c251b287c24bdd9 to your computer and use it in GitHub Desktop.
Save devrath/fd85861a423e37d18c251b287c24bdd9 to your computer and use it in GitHub Desktop.
This is a demo of rememberUpdated state in kotlin for jetpack compose
class RememberUpdatedStateActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { CurrentScreen(){
// Control is received back after performing the long running operation for a certain duration
} }
}
@Composable
fun CurrentScreen(
onCallback: ()-> Unit
){
// Using the remember updated state we can prevent this and make this composable to be reused from other place also
val rememberUpdatedState by rememberUpdatedState(newValue = onCallback)
// We know that below block of code is executed only once,
// Since we use LaunchedEffect and have set the key to true
LaunchedEffect(key1 = true, block = {
delay(2000L)
rememberUpdatedState()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment