Instead of creating memory leaks (by holding the activity context in the class field), you can try this solution because general settings do not need an activity context, but ... any context :). For long live objects you should use ApplicationContext.
Create an application class:
public class MySuperAppApplication extends Application { private static Application instance; @Override public void onCreate() { super.onCreate(); instance = this; } public static Context getContext() { return instance.getApplicationContext(); } }
Register it in the manifest
<application ... android:name=".MySuperAppApplication" > ... </application>
Then you can do something like this
public void persistItems(Information info) { Context context = MySuperAppApplication.getContext(); SharedPreferences sharedPreferences = context.getSharedPreferences("urlPersistencePreferences", Context.MODE_PRIVATE); sharedPreferences.edit() .putString("Url", info.Url) .putString("Email", info.Email); }
The method signature looks better because it does not need an external context. This can be hidden under some interface. You can also easily use it for dependency injection.
NTN
source share