I am making an app and I am using Android Room Persistence Library to handle my database layer. Room Library works like charm and everything is fine with it. But I want the database that room creates to be removed when the user uninstall the app. I tried uninstalling the app and then installed again, but somehow the database was still there and app was able to get the old data from it.
I thought maybe because my app data backup is set to auto in the settings and android is backing it up on cloud and bringing it back again but turning off backup from settings didn't help. Even if that worked that doesn't sound like a good solution to me.
I have created a very simple class that extends RoomDatabase
and below is the code if that helps answering the question.
I know that I can use fallbackToDestructiveMigration()
on the database builder and increase the database version. It will clear the data from the database. That is not what I want.
@Database(entities = {UnleashedEntity.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract DaoContract MyDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
Edit
I know I can use ACTION_PACKAGE_REMOVED
intent to make my app aware of the uninstall. What I wanted to know, Is there a configuration of the databasebuilder
that does the job, and How come the database persists after app uninstall.
Generally No actually. When you uninstall, the APK itself (/data/app/com. example. app-1.
3.1 Add the Clear all data menu option In the Options menu, select Clear all data. All words should disappear. Restart the app. (Restart it from your device or the emulator; don't run it again from Android Studio) You should see the initial set of words.
u can delete database manually by clear Data . settings\applications\manage Applications\'select your application'\clear data. this will delete more than the database.
Is there a configuration of the databasebuilder that does the job
AFAIK room does not know about app getting uninstall so Room
probably won't wipe the database for you on uninstall.
How come the database persists after app uninstall
Beginning with Android 6.0 (API level 23), Android offers the Auto Backup feature which is enabled by default. According to the documentations, it backup the following to google drive:
This backup your room database too.
How to disable auto backup
In your manifest:
<manifest ... >
...
<application android:allowBackup="false" ... >
...
</application>
</manifest>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With