Recently, the Android Jetpack Components came to light to guide us through our development road.
And using it gives us the ability to find out solutions to any problem and opens new ways of thinking.
The last couple of day I was working on an android app,
It had a service that generates some kind of string at the first launch of the app before displaying anything to the user.
I thought a lot about the available solutions we have such as broadcast receivers, Rx or Event bus.
All of them are good solutions but I want to make it simpler,
So I decided to Live Data to make it simple and Live _, Let's go quickly through how Live Data work.
Live Data is a data holder class that can be observed on any type of data within a given lifecycle.
This observer will be notified of any modification of the data if the Lifecycle Owner is in an active state.
What is Active state:- It means thereβs a LifecycleOwner listening to the data modification, and the Live Data will release when the LifecycleOwner goes to InActive State
What is InActive state:- It doesn't mean there`s noLifecycleOwner observing On the data Modification.
But it means the states of LifecycleOwner are either Lifecycle.State.STOPPED or Lifecycle.State.Destroy.
To learn more about Live Data, I recommend this article When And why use LiveData?.
Let`s go-to implementation directly.
Let us create a class and make it extend Live Data with a return value of String with return value String, then pass in the constructor the SharedPreferences Class, will look like this:
class TokenSharedPreferenceLiveData(private val sharedPreferences: SharedPreferences) : LiveData<String>() {
}
THEN we need to listen to changes of a specific SharedPreferences key and emit the changes to Live Data, so we create a variable to listen to changes and emit it if any exist, it will look like this below,
private val mTokenSharedPreferenceListener =
SharedPreferences.OnSharedPreferenceChangeListener({ sharedPreferences: SharedPreferences?, key: String? ->
if (key == MYKEYSTRING) {
value = sharedPreferences?.getString(MYKEYSTRING, "")
}
})
Finally, we need to register the listener if our class is in the onActive state and unregister it if it's in the onInActive State.
override fun onActive() {
super.onActive()
value = sharedPreferences.getString(MYKEYSTRING, "")
sharedPreferences.registerOnSharedPreferenceChangeListener(mTokenSharedPreferenceListener)
}
override fun onInactive() {
super.onInactive()
sharedPreferences.unregisterOnSharedPreferenceChangeListener(mTokenSharedPreferenceListener)
}
The class will look like this at the end:
class TokenSharedPreferenceLiveData(private val sharedPreferences: SharedPreferences) : LiveData<String>() {
private val mTokenSharedPreferenceListener =
SharedPreferences.OnSharedPreferenceChangeListener({ sharedPreferences: SharedPreferences?, key: String? ->
if (key == MYKEYSTRING) {
value = sharedPreferences?.getString(MYKEYSTRING, "")
}
})
override fun onActive() {
super.onActive()
value = sharedPreferences.getString(MYKEYSTRING, "")
sharedPreferences.registerOnSharedPreferenceChangeListener(mTokenSharedPreferenceListener)
}
override fun onInactive() {
super.onInactive()
sharedPreferences.unregisterOnSharedPreferenceChangeListener(mTokenSharedPreferenceListener)
}
companion object {
private const val MYKEYSTRING = "my_key_string"
}
}
The final step is to pass this class to our ViewModel class:
class MainActivityViewModel (private val tokenSharedPreferenceLiveData:
TokenSharedPreferenceLiveData
):ViewModel{
fun getTokenLiveData() = tokenSharedPreferenceLiveData
}
And in our Main Activity just listen to Changes.
mainActivityViewModel.getTokenLiveData().observe(this, Observer {
//do whatever you want
})
Conclusion
We have done a small task with simple usage of Live Data, you can do much more than this. I hope you find this helpful to you and feel free to add any info I missed.
Have a nice coding :)
Top comments (0)