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

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

Testing: how hard can it be?

When people start looking into the testing domain, very similar questions arise: What to test? And more important, what not? What should I mock? What should I test with unit tests and what with Instrumentation?…
Watch Video

Testing: how hard can it be?

DANNY PREUSSLER
Android Lead
Soundcloud

Testing: how hard can it be?

DANNY PREUSSLER
Android Lead
Soundcloud

Testing: how hard can it be?

DANNY PREUSSLER
Android Lead
Soundcloud

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
In this part of the series, we will plan our first screen in Jetpack…
READ MORE
blog
We’ll be selecting a time whenever a user presses a number key. Following points…
READ MORE
blog
Ask yourself a fairly standard question for any interview to fill an Android position:…
READ MORE
blog
This is part of a multi-part series about learning to use Jetpack Compose through…
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