Reputation: 233
How to call getApplicationContext
in static method in class that extends Application class?
I want to create Database class inside AppContainer class, but I don't know from where get context for Database constructor.
Database
public class Database extends SQLiteOpenHelper {
public Database(Context context) {
super(context, "database.db", null, 1);
}
}
AppContainer
public class AppContainer extends Application{
// Problem---------|
private static class DatabaseHolder { // v
public static final Database DATABASE = new Database(getApplicationContext());
}
public static Database getDatabase()
{
return DatabaseHolder.DATABASE;
}
}
==================EDIT
I made error in my code. I forgot write word "static" at getDatabase method. Error has been improved. I don't want to create AppContainer in my code. I want use this class as container with static fields and get them without create AppContainer's instance.
I found solution of my problem:
public class AppContainer extends Application {
private static AppContainer instance;
private static Database database;
public AppContainer() {
instance = this;
}
public static AppContainer getInstance() {
if (instance == null)
synchronized (AppContainer.class) {
if (instance == null)
instance = new AppContainer();
}
return instance;
}
public static Database getDatabase() {
if (database == null)
synchronized (AppContainer.class) {
if (database == null)
database = new Database(getInstance().getApplicationContext());
}
return database;
}
}
Upvotes: 2
Views: 6966
Reputation: 664
Try creating a static variable of Context type and initialize it in the constructor and hence all that static variable in the method. Like this :
public class AppContainer extends Application {
private static AppContainer instance;
private static Context ctx;
private static Database database;
public AppContainer() {
...
ctx=getApplicationContext();
}
...
public static Database getDatabase() {
....
Database database = new Database(ctx);
return database;
}
}
Upvotes: 1
Reputation: 55380
You probably don't need to initialize the Database
object as part of the AppContainer
.
In particular since your method
public Database getDatabase()
{
return DatabaseHolder.DATABASE;
}
is not static, it cannot be called unless the Application
object is fully initialized. Therefore, you don't need a static reference to it -- having it as a normal instance member, and initializing it in the constructor, should suffice.
public class AppContainer extends Application
{
private Database mDatabase;
public AppContainer()
{
super();
mDatabase = new Database(this);
}
public Database getDatabase()
{
return mDatabase;
}
}
If, however, you needed to make the getDatabase()
method static, then changing the mDatabase
instance field into a static field and keeping the rest the same would probably suffice. It's highly unlikely that you would want to access the database before an Application
object was available.
Upvotes: 1
Reputation: 7606
Where will you create the AppContainer
object? If you are creating that object in a class which extends to Activity
, you can pass the context when creating this object. Basically it looks like this:
public class AppContainer extends Application{
private Context c;
public AppContainer(Context context){
this.c = context;
}
.............
}
In the class which extends to Activity
:
public class theClass extends Activity{
AppContainer container = new AppContainer(this);
}
Upvotes: 0