Created
January 30, 2019 23:18
-
-
Save idish/f46a8327da7f293f943a5bda31078c95 to your computer and use it in GitHub Desktop.
I answer to myself with what I did:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferenceStringLiveData sharedPreferenceStringLiveData = new SharedPreferenceStringLiveData(preferences, "cid", "");
sharedPreferenceStringLiveData.getStringLiveData("cid", "").observe(this, cid -> {
Toast.makeText(this, "Change in CID "+cid, Toast.LENGTH_SHORT).show();
});
In SharedPreferenceLiveData, I included a new function :
public SharedPreferenceLiveData<String> getStringLiveData(String key, String defaultValue) {
return new SharedPreferenceStringLiveData(sharedPreferences, key, defaultValue);
}
Not sure this is the right way to do. In addition, I do not see why we need to pass key and defaultvalue in the constructor.
SharedPreferenceLiveData should extends MutableLiveData?, or am i wrong?
SharedPreferenceLiveData should extends MutableLiveData?, or am i wrong?
You don not need extend MutableLiveData. setValue and postValue are protected, so you can use them as long as you extend LiveData.
How do you use this with Transformation switchmap? I would like to use it with my ViewModel and the ViewModel needs to be aware of changes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I would like to implement your example above. How do you observe for the updates. Do you have an example of code I could use to put in my activity / fragment ?
I do not see any function returning a livedata I could observe. (Ok I must admit I'm not an expert :-)
Thx for your help.