Search code examples
androidkotlinandroid-jetpack-composeandroid-room

Room DB is Destroyed When App Restarts in Android 15 Pixel 9


I'm facing an issue where the Room Database is destroyed when my app is restarted on an Android 15 (Pixel 9) device. After the app is closed and reopened, the database doesn't persist, and I lose all stored data.

    @Provides
    @Singleton
    fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
        return Room.databaseBuilder(
            context.applicationContext,
            AppDatabase::class.java,
            "app_database"
        ).createFromAsset("database/external_database.db")
            .enableMultiInstanceInvalidation()
            .fallbackToDestructiveMigration()
            .build()
    }

It only happens when i prepopulate database .createFromAsset("database/external_database.db").


Solution

  • Recommended value is true as otherwise Room could leave obsolete data when table names or existence changes between versions.

    fallbackToDestructiveMigration(boolean dropAllTables)
    

    refer more details fallbackToDestructiveMigrationFrom