When we add new shared preference key value details for new version android app and When we add column to existing sqlite database in android will it automatically creates these all details when old user updates from play-store?
-
I think app should be reinstalled – MMG Mar 17 '20 at 07:21
-
But user need to uninstall old one before installing new one..But I do not want to uninstall old one – Manju Mar 17 '20 at 07:22
-
I think because of changing in database, app should be reinstalled – MMG Mar 17 '20 at 07:23
-
how user will get to know that ? If there are n number of users then it is not possible inform right ? any other solution? – Manju Mar 17 '20 at 07:26
-
When you notify user that new update has come, when he clicks to update, you can replace new one with last one – MMG Mar 17 '20 at 07:28
-
1@MohammadMoeinGolchin If you don't know about the features then don't share wrong info with other – Ali Mar 17 '20 at 07:29
-
It means we need to handle in source code? – Manju Mar 17 '20 at 07:30
-
If you change your database structure in an update, you can override [SQLiteOpenHelper's onUpgrade](https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade(android.database.sqlite.SQLiteDatabase,%20int,%20int)) method in order to handle database updates properly. Please see this link also [Android SQLite database and app update](https://stackoverflow.com/questions/16890774/android-sqlite-database-and-app-update) @Manju You need to more R&D on that. – Ali Mar 17 '20 at 07:31
-
how about shared preference? – Manju Mar 17 '20 at 07:32
-
It is possible for one question to have more than one correct answer @Ali, we should respect to each other – MMG Mar 17 '20 at 07:35
-
@MohammadMoeinGolchin Yes buddy but do some R&D on that first then share info with other that's it bro. Sorry if I break your feelings :) – Ali Mar 17 '20 at 07:36
2 Answers
If you are installing the new version of existing application Shared Preferences will give no error but in SQLite, you need to change the version of DB also otherwise new changes will not reflect back until you re-install it again, but it will truncate all the existing data in SQLite. So it is better to change the version.
Version is mentioned inside the constructor of your Database class. For example,
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 6);
mContext = context;
}
In this the 4th paramter is version

- 1,129
- 1
- 8
- 27
Shared preferences persist until you completely un-install the application.
And as far as database is concerned, you can make changes in it. There is a concept known as "database migration".
If you are using Room, this might help, https://developer.android.com/training/data-storage/room/migrating-db-versions
If you are using simple Sqlite without Room, you have to make changes in the onUpgrade()
function,
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// add Column here
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE foo ADD COLUMN new_column INTEGER DEFAULT 0");
}
}

- 536
- 5
- 19