Created
January 22, 2023 13:24
-
-
Save devrath/fd85861a423e37d18c251b287c24bdd9 to your computer and use it in GitHub Desktop.
This is a demo of rememberUpdated state in kotlin for jetpack compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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