kotlin - Room DB is Destroyed When App Restarts in Android 15 Pixel 9 - Stack Overflow

admin2025-04-17  2

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").

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").

Share Improve this question edited Feb 1 at 3:31 Santhosh Kumar asked Feb 1 at 3:11 Santhosh KumarSanthosh Kumar 5611 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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

转载请注明原文地址:http://anycun.com/QandA/1744840691a88357.html