0
0
Android Kotlinmobile~5 mins

SharedFlow for events in Android Kotlin

Choose your learning style9 modes available
Introduction

SharedFlow helps send messages or events to many parts of your app at the same time. It is great for things like showing messages or navigation commands.

You want to show a message (like a toast) after a button click.
You need to tell multiple screens about a change in data.
You want to handle one-time events like navigation or alerts.
You want to share events from a ViewModel to UI components.
You want to avoid missing events when the app screen changes.
Syntax
Android Kotlin
val eventFlow = MutableSharedFlow<Event>()
suspend fun sendEvent(event: Event) {
  eventFlow.emit(event)
}

// To collect events
lifecycleScope.launch {
  eventFlow.collect { event ->
    // handle event
  }
}

MutableSharedFlow is used to send events.

Use emit() to send an event and collect() to receive it.

Examples
Sends a string message as an event to show a toast.
Android Kotlin
val toastEvent = MutableSharedFlow<String>()
suspend fun showToast(message: String) {
  toastEvent.emit(message)
}
Collects the toast messages and shows them on screen.
Android Kotlin
lifecycleScope.launch {
  toastEvent.collect { message ->
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
  }
}
Sends navigation commands as events.
Android Kotlin
val navigationEvent = MutableSharedFlow<NavigationCommand>()
suspend fun navigate(command: NavigationCommand) {
  navigationEvent.emit(command)
}
Sample App

This app shows a button. When you tap it, a message appears as a toast using SharedFlow to send the event.

Android Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
  private val toastEvent = MutableSharedFlow<String>()

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val button = Button(this).apply { text = "Click me" }
    setContentView(button)

    lifecycleScope.launch {
      toastEvent.collect { message ->
        Toast.makeText(this@MainActivity, message, Toast.LENGTH_SHORT).show()
      }
    }

    button.setOnClickListener {
      lifecycleScope.launch {
        toastEvent.emit("Button clicked!")
      }
    }
  }
}
OutputSuccess
Important Notes

SharedFlow with default settings (replay = 0) does not deliver previous events to new collectors, making it perfect for one-time events.

Use MutableSharedFlow(replay = 0) for one-time events without replay.

Always collect SharedFlow in a lifecycle-aware scope to avoid leaks.

Summary

SharedFlow is great for sending events to multiple listeners.

Use emit() to send and collect() to receive events.

It helps handle UI events like toasts and navigation safely.