I have a litte database with Android Room. I want prepopulate the tables with some data but I can't get trigger the callback with onCreate
or onOpen
methos.
I have the Database with singleton pattern like this:
public class DatabaseSoundsInitializer{
...
private static DatabaseSounds databaseObject;
public static DatabaseSounds getDatabaseSoundsInitializer(Context context, Class klass, String name){
if(databaseObject == null){
Log.d(TAG, "Debug: Creating the database");
databaseObject = (DatabaseSounds) Room.databaseBuilder(context, klass, name).addCallback(rdc).build();
}
return databaseObject;
}
static RoomDatabase.Callback rdc = new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
Log.d(TAG, "Debug: onCreate");
}
@Override
public void onOpen(@NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
Log.d(TAG, "Debug: onOpen");
}
};
...
}
I call this from my main Activity like this:
DatabaseSounds db = DatabaseSoundsInitializer.getDatabaseSoundsInitializer(getApplicationContext(), DatabaseSounds.class, Utils.DATABASE_NAME);
Here my database Class:
public abstract class DatabaseSounds extends RoomDatabase{}
I can see the log with Debug: Creating the database but not others. I am making this wrong?