Blog Infos
Author
Published
Topics
,
Published
Topics
,

LiveData with SnackBar, Navigation and other events (the SingleLiveEvent case)

Background
class MyViewMode: ViewModel() {
data class ViewState(
val someUIProperty: String = "",
val someOtherUIProperty: Int = 1,
)
private val _viewState = MutableStateFlow<ViewState>(ViewState())
val viewState = _viewState.asStateFlow()
}
view raw gistfile1.txt hosted with ❤ by GitHub

Some developers prefer LiveData but whatever your choice the UI state is exposed by the ViewModel as an observable property that can be observed by the view during whatever lifecycle state might be convenient. Something like this perhaps:

// In your view/fragment
viewLifecycleOwner.lifecycleScope.launch {
viewModel.viewState
.flowWithLifecycle(viewLifecycleOwner.lifecycle, Lifecycle.State.STARTED)
.collect {
// do something with the UI updates
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Finally, as a recap, I had previously implemented events using a channel received as a flow. (It’s this event channel we’re going to remove/update.) For example:

// In your view model
private val _eventChannel = Channel<Event>(Channel.BUFFERED)
val events = _eventChannel.receiveAsFlow()
view raw gistfile1.txt hosted with ❤ by GitHub
Requirements for Events
Google’s New Guidance

UserMessage events

Job Offers

Job Offers


    Android Engineer

    American Express
    Phoenix, USA
    • Full Time
    apply now

    API Engineer

    American Express
    London
    • Full Time
    apply now

    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

Jobs

In the above example, the userMessages property of the UI state is the collection of transient messages to be displayed on the UI as a toast or as a snackbar, for example. In Google’s sample, they are adding to that collection when they want a message (event) to be displayed in the UI and removing a value from that collection when the message (event) has been displayed.

The ViewModel emitting an event

 

As you can see, from their example, emitting an event is just adding it to a list that is part of the UI state. In the view (fragment or activity) they are observing the view state to receive those one-shot events during safe lifecycle states:

The View’s observer of events

 

The ViewModel function to remove an event from the UI state

Some Notes
Unique Identifiers
sealed class Event {
    val uniqueId: String = UUID.randomUUID().toString()
    
    data class MessageEvent(val message: String): Event()
    object MarkerEvent: Event()
}
Busy Looping
Mandatory Callback
Is this better?

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
As part of a recent project, I decided to utilize Jetpack Compose for my…
READ MORE
blog
Jetpack Compose recently got released to production and its release was accompanied with great…
READ MORE
blog
State hoisting is an important concept useful to make a Composable stateless (and so…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu