Fill the blank
Read the ViewModel's state in a lifecycle-aware way.
@Composable
fun NotesRoute(vm: NotesListViewModel) {
val notes by vm.notes.____()
Column { notes.forEach { Text(it) } }
}Hint 1 of 1
Which one knows about the lifecycle?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A collectAsStateWithLifecycle
Correct. It stops collecting below STARTED.
Why not the others?
- B collectAsState. It compiles, but keeps collecting while the app is in the background.
- C observeAsState. That's for LiveData.
- D collect. collect suspends and returns nothing; you can't call it in a composable body.