This is probably a stupid question. Im developing an app that has themes, The user can choose a theme for the app
The themes are values stored in an enum
Enum Theme{light, dark, rose, violet,.... }
How to store the enum value for the app to remember on launch?
As Tenfour04 already said - SharedPreferences
are probably the correct store for something like this. Since your Enum
is in fact an int
, you can store it in there using SharedPreferences.Editor
's putInt
method and afterwards receive it with SharedPreferences
's getInt
method.
Also refer to https://developer.android.com/reference/android/content/SharedPreferences
This is written without IDE, but should be quite close.
// save it
SharedPreferences sharedPreferences = context.getSharedPreferences("userdetails", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("theme",(int)Theme.light);
bool success = editor.commit();
// get it again (2nd parameter is default value)
Theme selectedTheme = (Theme) sharedPreferences.getInt("theme", (int) Theme.dark)
EDIT: An Enum
is in java not an int
- sorry, I thought of .NET. Anyhow it is common practice to give an Enum
an identifier ( int
) field. Using this field, you can serialize the selected value into the SharedPreferences
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.