29

I have been using the SingleLiveData class which can be found here. My questions are:

  1. Is SingleLiveData is actually part of the Android Architecture Components?

  2. Is it a good idea to use it?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Archie G. Quiñones
  • 11,638
  • 18
  • 65
  • 107
  • 3
    You can read this https://medium.com/google-developers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150 – Raghunandan Aug 10 '18 at 09:54

3 Answers3

40

Is SingleLiveEvent actually part of the Android Architecture Components Library?

No, and it won't be: https://issuetracker.google.com/issues/122413110. Basically, the official answer is "Yes, regular livedata wasn't enough, so we introduced it in our examples but it's too hacky to be in the library".

Ghedeon
  • 1,643
  • 1
  • 18
  • 30
14

Looking at Live data and its Parent/Children inheritance there is no such thing as SingleLiveData. Reading link provided by @Raghu, I find statement:

The SingleLiveEvent class was created for a sample as a solution that worked for that particular scenario. It is a LiveData that will only send an update once.

So to answer your questions:

Is SingleLiveData is actually part of the Android Architecture Components?

No, it is not!

Is it a good idea to use it?

This depends on many factors. Since I haven't used it I will give general idea. Using classes that you find in demo app or other way might not be guaranteed to be updated or bug-fixed. So if you understand the class so well that you can fix any bug you might find, and if it fits your need, then use it.

But generally I would avoid that if I can find something that is well maintained and does the same job.

Since from skimming the article I get impression he is trying to make some sort of observer pattern, I will suggest you check out rxjava

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • *I will suggest you check out rxjava* 1) that completely beats the purpose of using livedata 2) the argument about bug fixed and maintenance doesn't hold much weight in this case considering the class is like 10 lines long – Tim Apr 09 '19 at 08:20
  • 2
    Isn't SingleLiveData an anti-pattern? – Cassio Landim Jul 02 '20 at 19:27
0

I'm using ObservableField for single UI events but it requires some hacking to use with Fragments to subscribe/unsubscribe automatically.

Arthur
  • 3,636
  • 5
  • 19
  • 25