0
0
Android Kotlinmobile~10 mins

SharedFlow for events in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a MutableSharedFlow for events.

Android Kotlin
val eventFlow = MutableSharedFlow<[1]>()
Drag options to blanks, or click blank then click option'
AString
BBoolean
CInt
DFloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using a numeric type when events are text messages.
Forgetting to specify the type parameter.
2fill in blank
medium

Complete the code to emit an event string into the SharedFlow.

Android Kotlin
viewModelScope.launch {
  eventFlow.[1]("ButtonClicked")
}
Drag options to blanks, or click blank then click option'
Acollect
Bsubscribe
Cemit
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect which is for receiving, not sending.
Using send which is for Channels, not SharedFlow.
3fill in blank
hard

Fix the error in collecting events from the SharedFlow in a lifecycle-aware way.

Android Kotlin
lifecycleScope.launchWhenStarted {
  eventFlow.[1] { event ->
    handleEvent(event)
  }
}
Drag options to blanks, or click blank then click option'
Asubscribe
Bemit
Claunch
Dcollect
Attempts:
3 left
💡 Hint
Common Mistakes
Using emit which is for sending events.
Using subscribe which is not a Kotlin Flow function.
4fill in blank
hard

Fill both blanks to create a SharedFlow that replays the last event and has no extra buffer.

Android Kotlin
val eventFlow = MutableSharedFlow<String>(replay = [1], extraBufferCapacity = [2])
Drag options to blanks, or click blank then click option'
A1
B0
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Setting replay to zero loses the last event for new subscribers.
Setting extraBufferCapacity too high can cause unexpected buffering.
5fill in blank
hard

Fill all three blanks to collect events from SharedFlow in a Compose UI using LaunchedEffect.

Android Kotlin
LaunchedEffect(Unit) {
  eventFlow.[1] { event ->
    when(event) {
      [2] -> showToast("Clicked")
      [3] -> showToast("Error")
    }
  }
}
Drag options to blanks, or click blank then click option'
Acollect
B"ButtonClicked"
C"ErrorOccurred"
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using emit instead of collect to receive events.
Using incorrect event string literals in the when statement.