Note: I am exhausted in the middle of my ambition
The standard API SharedPreference is nice! It depends on the scale of the application, but it is very reliable when you like "DB and messed up !!!!!!". Since the primitive type is suppressed, small flags can be saved. Even if I update the app, it's crap. Since it is saved in xml, you can easily see the contents. You did it!
Here is an app that manages settings with SharedPreference. The number of keys is over 100. Keys are scattered all over the place, such as being written in a constant class, being made a constant in Activity, or being written directly.
sharedPreference.getAll.toString()
If you manage to look inside the Shared Preference, you will close it. From now on you have to grep the keys one by one, pick them up and decipher the meaning of the keys and values. It's hard.
--Overwrite an existing key that you don't even know where it is inadvertently --I don't know how to retrieve the value I want because the key is dynamically generated. --The same value is saved three times with different keys --There is a SharedPrefUtil class for each screen (and stored in the same SharedPreference) --As a result of typoing the key name, only the default value is included at any time.
I think most of the problems caused by SharedPreference are due to key management. Even if you put them together as a constant class, if multiple people edit it, you may inadvertently define the same key as the existing one. It's a pain to have to go to the places I use to check the value type for that key.
As the title of the article. I want you to use ʻEnum.name () `.
All the keys are defined in the same enum class. You can see at a glance which key is defined.
If the definition name is covered, it will not compile in the first place, so you can rest assured.
If you create an enum class for each group of keys, the keys will not be mixed and messed up.
By the way, ʻEnum.name () instead of ʻEnum.toString ()
is because @Override
cannot be done.
It's quite possible that someone kind will override toString and explain what's inside.
I personally wish I could do this! The enum for Shared Preference is as follows.
FoodPref.java
import android.content.Context;
import android.content.SharedPreferences;
enum FoodPref {
//Octopus
TAKO(100),
//Cat
NEKO(200),
//There
SOKO(300);
//Default value.
public final Object DEF;
FoodPref(Object def) {
this.DEF = def;
}
//Shared Preference for this enum only
public static SharedPreferences getThisPreference(final Context context) {
return context.getSharedPreferences(FoodPref.class.getSimpleName(), Context.MODE_PRIVATE);
}
}
How to use
//Create
SharedPreferences foodSp = FoodPref.getThisPreference(context);
//Put out
int cat = foodSp.getInt(FoodPref.NEKO.name(), (Integer) FoodPref.NEKO.DEF);
//Put in
foodSp.edit().putInt(FoodPref.SOKO.name(), 666).apply();
//Even if it increases in various ways
SharedPreferences toySp = ToyPref.getThisPreference(context);
//Because it is saved separately
SharedPreferences carSp = CarPref.getThisPreference(context);
//There is no risk of mixing values
SharedPreferences bookSp = BookPref.getThisPreference(context);
Default value is Object "I know for sure which type corresponds to that key." "Don't hesitate about the default value at the time of acquisition" Considering the merit, I think that the direction of setting the default value for the key is correct, but It's unforgivable that the caller needs a cast. Null cannot be the default value. If you group SharedPreferences by value type, you can fix the return value for the time being, but if possible, I would like to group the keys with enum as a group of meanings. I tried it in Kotlin but it just changed Object to Any. Is there any way if I am proficient?
Still, I'll put the key directly in the String! It may be better to create a wrapper class that takes only the defined enum as an argument.
I want to take over the price from an existing project I have no choice but to do my best
I'd be happy if someone could do something better!
Recommended Posts