Cheok Yan Cheng Cheok Yan Cheng - 3 years ago 459
Android Question

Save enum to SharedPreference

I was wondering, what is the common method being used to save

? Currently, I'm using
to convert enum to String, and then save it to

Gson gson = new Gson();
// country is an enum.
String json_country = gson.toJson(country);
sharedPreferences.edit().putString(COUNTRY, json_country);

I was wondering, is this a good way? Is there any better way?

Answer Source

You can assotiate your enums with integers and store simple int, look this:

Cast Int to enum in Java (second answer) - also in same way you can do enumToInt

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download