Search code examples
androidandroid-architecture-componentsandroid-livedatamutablelivedata

Is SingleLiveEvent actually part of the Android Architecture Components Library?


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?


Solution

  • 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