Always use the application context when instantiating singletons

This commit is contained in:
cketti 2012-05-16 22:22:39 +02:00
parent c3808827a8
commit bd6cdecdb0
3 changed files with 7 additions and 4 deletions

View File

@ -25,8 +25,9 @@ public class Preferences {
private static Preferences preferences;
public static synchronized Preferences getPreferences(Context context) {
Context appContext = context.getApplicationContext();
if (preferences == null) {
preferences = new Preferences(context);
preferences = new Preferences(appContext);
}
return preferences;
}

View File

@ -31,6 +31,7 @@ public abstract class Contacts {
* @return Appropriate {@link Contacts} instance for this device.
*/
public static Contacts getInstance(Context context) {
Context appContext = context.getApplicationContext();
if (sInstance == null) {
/*
* Check the version of the SDK we are running on. Choose an
@ -41,9 +42,9 @@ public abstract class Contacts {
* The new API was introduced with SDK 5. But Android versions < 2.2
* need some additional code to be able to search for phonetic names.
*/
sInstance = new ContactsSdk5p(context);
sInstance = new ContactsSdk5p(appContext);
} else {
sInstance = new ContactsSdk5(context);
sInstance = new ContactsSdk5(appContext);
}
}

View File

@ -18,11 +18,12 @@ public class TracingPowerManager {
private Timer timer = null;
public static synchronized TracingPowerManager getPowerManager(Context context) {
Context appContext = context.getApplicationContext();
if (tracingPowerManager == null) {
if (K9.DEBUG) {
Log.v(K9.LOG_TAG, "Creating TracingPowerManager");
}
tracingPowerManager = new TracingPowerManager(context);
tracingPowerManager = new TracingPowerManager(appContext);
}
return tracingPowerManager;
}