I made Cache
class that use SharedPreferences
.
It contains below methods.
static save(Context context, String key, Mixed value)
(Mixed: String, long, int, double, ...)static load[Type](Context context, String key)
(loadString, loadInt, loadDouble, ...)static exists(Context context, String key)
These methods contain a Context
parameter.
Because Context
is necessary to use SharedPreferences
.
I think it is very uncomfortable.
So I modify the class like below.
private static Context context
static init(Context context)
: savecontext
tostatic context
static save(String key, Mixed value)
static load[Type](String key)
static exists(String key)
or
private static SharedPreferences sp
static init(Context context)
- save
context.getSharedPreferences(...)
tostatic sp
- save
static save(String key, Mixed value)
static load[Type](String key)
static exists(String key)
init
method will call at SplashActivity
like Cache.init(getApplicationContext())
.
(SplashActivity
is Launcher Activity)
This is a safe way? or is any better way?