Byte 4 – LiveEvents

Some time ago we got to the conclusion that we should migrate from our MVP architecture to a MVVM one. There were a lot of things to learn and discover. Expect that the following bytes will focus more on this.

One of the problems that I faced was that sometimes I would like the View to only get notified only once of an event. More precisely: upon user re-entering the screen or the configuration change I wouldn’t like the event triggered again.

Case

I’m on a screen that displays a list. When tapping on a cell we will display a BottomDialogFragment. When tapping one of the actions in the DialogFragment we would navigate away from the screen.

The problem? When going away from the screen, hitting back, and having a configuration change, then the DialogFragment would be displayed again.

LiveEvent to the rescue

LiveEvent is a class that emits the event only once and not after other configuration change events. So basically the DialogFragment would only be displayed once!

If you wonder where this would come in handy:

  • navigating to a new Fragment or Activity
  • displaying an AlertDialog
  • showing a BottomDialogFragment

For this purpose I’ve used is this library. It extends LiveData and it’s basically identical to use. Highly recommended!




Leave a Comment

Blog

Recent posts