This Android project focuses on demonstratinq the implementation of LiveData and ViewModel to facilitate data management and UI updates.
The project consists of two key components: MainViewModel
and its corresponding unit test.
The MainViewModel
class, located in the com.shrey.live_data
package, serves
as the central data handler for the application. It extends the ViewModel
class from the Android Architecture Components library and utilizes LiveData to
manage the current galaxy name.
LiveData is employed to encapsulate the galaxy name data and ensure that
changes to the data are observed by any UI components that are interested. The
ViewModel exposes a LiveData object named galaxyLive
, which holds the current
galaxy name. Additionally, a function named updateLiveData()
is provided to
update the LiveData value.
To integrate LiveData into your Android project:
- Create a ViewModel class that extends the ViewModel class.
- Use MutableLiveData to store and manage data that needs to be observed.
- Expose LiveData objects to allow UI components to observe data changes.
- Update data using MutableLiveData's
setValue()
orpostValue()
methods.