Search code examples
android-room-prepackageddatabase

How to create prepopulated DB only at first start of android application?


Hellow eweryone! I'm begginer at Room and I need to create DB with starting data at first starting of App and then modify data in app and use modifyed data at second starting of App.

I have this code

companion object {
    @Volatile
    private var INSTANCE: AppDatabase? = null

    fun getDatabase(context: Context): AppDatabase {
        return INSTANCE ?: synchronized(this) {
            val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_database.db",
                )
                    .createFromAsset("database/01-01-2024.db")
                    .build()
        
            INSTANCE = instance

            instance
        }
    }

It creates prepopulated DB successfully and I can modify data in next screen of my app. But when i close the app and open second time the DB with my modified data rewrites with prepopulated DB.

How can i use prepopulated DB only when BD is not exists?

P.S. I was watching documentation but it didn't help.


Solution

  • What a shame! I didn't check the clear data checkbox in my run configuration enter image description here