2

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): save context to static 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(...) to static sp
  • 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?

TaeSang Cho
  • 245
  • 3
  • 12

0 Answers0