Grunden til OnCreate bliver ikke kaldt, fordi den kun kaldes én gang, når databasen først oprettes, og derefter aldrig igen, så længe databasen eksisterer.
Hvis du sletter appens data eller afinstallerer appen og derefter kører den igen, vil du se, at onCreate kaldes så.
for eksempel.
private void BuildDatabase() {
RoomDatabase.Builder roombuilder = Room.databaseBuilder(this, Database.class,"mydb");
roombuilder.addCallback(new RoomDatabase.Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
Log.d("ONCREATE","Database has been created.");
}
@Override
public void onOpen(@NonNull SupportSQLiteDatabase db) {
super.onOpen(db);
Log.d("ONOPEN","Database has been opened.");
}
});
mRoomDB = (Database) roombuilder.build();
}
Efter sletning af appens data resulterer i:-
2018-12-20 06:36:23.045 2271-2287/so53839431.so53839431roomrelationship D/ONCREATE: Database has been created.
2018-12-20 06:36:23.055 2271-2287/so53839431.so53839431roomrelationship D/ONOPEN: Database has been opened.