Reputation: 519
I am stuck in a problem. I am creating a helper class that needs to be Singleton. And that class has a global context variable. I am not able to do this since context is available only from onCreate and this Singleton instance is created much before since it is static.
Can someone help me on how to solve this issue. Context is needed for the Singleton instance finally.
public class Helper {
private static Helper sHelper = new Helper() ;
private Helper () {} ;
public static Helper getInstance() {
return sHelper;
}
public boolean doSomething() {
mContext.getContentResolver;
return isDone;
}
}
Upvotes: 1
Views: 1308
Reputation: 39
You need pass the context trougth your construct helper class:
getApplicationContext()
Upvotes: 0
Reputation: 2530
You just need to pass the following context to your helper class.
getApplicationContext()
Upvotes: 0
Reputation: 7466
You can set the context to be you ApplicationContext. You can create an Application class and implement something like:
yourSingletonClass.getInstance().setContext(this);
This call should be in you application class under the onCreate method. For more information try this docs: Android - Application class
Upvotes: 2