Understanding side effects
In the Using Scaffold() to structure your screen section of Chapter 6, Putting Pieces Together, I showed you how to display a snack bar using rememberCoroutineScope {}
and scaffoldState.snackbarHostState.showSnackbar()
. As showSnackbar()
is a suspending function, it must be called from a coroutine or another suspending function. Therefore, we created and remembered CoroutineScope
using rememberCoroutineScope()
and invoked its launch {}
function.
Invoking suspending functions
The LaunchedEffect()
composable is an alternative approach for spawning a suspending function. To see how it works, let's look at the LaunchedEffectDemo()
composable. It belongs to the EffectDemo
sample, as illustrated in the following screenshot:
LaunchedEffectDemo()
implements a counter. Once the Start button has been clicked, a counter is incremented every second. Clicking on...