[Library] StoreBox, an open-source Android library for streamlining SharedPreferences topic
When getting a value from any preferences, whether private Activity or default shared preferences, you would normally have to get a reference to a SharedPreferences instance, for example using
Only after that we can start getting the values out, such as
This can get reasonably tedious if the preferences need to be accessed from multiple places, and the API always requires us to pass a key and a default value. This can get error prone quickly, and increases with the amount of keys and default values that need to be used. Putting commonly accessed preferences behind a wrapper is a reasonable solution, but still requires some boilerplate code.
What if however we could define an interface like
for everyone to use in order to save and get values?
With StoreBox that becomes reality. Given the above interface definition you can easily create an instance of the interface using
and you will be able to retrieve the value just by calling the defined method
You can read more about the project here, including details on how it can be used and added to a project.
Let me know what you think and whether you find it useful. Thanks!
Code:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(someContext);
Only after that we can start getting the values out, such as
Code:
String myValue = prefs.getString("someKey", "someDefaultValue");
This can get reasonably tedious if the preferences need to be accessed from multiple places, and the API always requires us to pass a key and a default value. This can get error prone quickly, and increases with the amount of keys and default values that need to be used. Putting commonly accessed preferences behind a wrapper is a reasonable solution, but still requires some boilerplate code.
What if however we could define an interface like
Code:
public interface MyPreferences {
@KeyByString("someKey")
String getSomeValue();
}
for everyone to use in order to save and get values?
With StoreBox that becomes reality. Given the above interface definition you can easily create an instance of the interface using
Code:
MyPeferences prefs = StoreBox.create(context, MyPeferences.class);
and you will be able to retrieve the value just by calling the defined method
Code:
String myValue = prefs.getSomeValue();
You can read more about the project here, including details on how it can be used and added to a project.
Let me know what you think and whether you find it useful. Thanks!
xda-developers
- Is root required to roll back to KitKat? topic
- [REQ] Multi DPI Stock Camera topic
- May be this is it for Xperia J topic
- [Q] No IMEI and baseband anymore. So back to stock rom for repair! topic
- [Q] New to phone - on 24A rooted - where to go from here? topic
- Htc desire 816 official lollipop update video review topic
- [Q] Galaxy S6 Edge T-Mobile Unlock by SIM code topic
- How to roll back to KK topic
- What is this in Greenify settings topic
- [Q] How to update? topic
- [Library] StoreBox, an open-source Android library for streamlining SharedPreferences topic
0 commentaires:
Enregistrer un commentaire