0

In my current project's previous version we were using database for settings. but for now we think that database is overhead for simple key value pairs so we decided to use sharedpref for that. Now the problem is how to handle to update functionality. we are not using database so onUpgrade will not be work.

I am planing to do

ArrayList<String> databases = new ArrayList<String>(Arrays.asList(mContext.databaseList()));
if(databases.contains("dbname")){
    copyDataToSharedPref();
    mContext.deleteDatabase("dbname");
}

is there any simple way to handle this?

Vivart
  • 14,900
  • 6
  • 36
  • 74
  • http://stackoverflow.com/questions/12637737/what-will-happen-to-the-sharedpreferences-on-update-an-android-app – SMR Jun 19 '14 at 11:02

2 Answers2

2

is there any simple way to handle this?

Not exactly but it isn't difficult. Just do something similar to the following...

  1. When your main / launcher Activity starts get it to check SharedPreferences for a specific key, example - a boolean "update_complete".
  2. If the key doesn't exist, there are two possibilities. The first is it's a clean (new) install, the second is it's an update.
  3. If the key DOES exist then the app has already been updated and the database data has already been transferred to SharedPreferences so proceed to run the app.
  4. If the key DOESN'T exist then check to see if the database exists. If the database DOESN'T exist then this is a new installation. In that case just setup SharedPreferences as a new installation.
  5. If the key DOESN'T exist but the database DOES exist, transfer the data from the database into SharedPreferences, delete the database then put the "update_complete" boolean into SharedPreferences.

I think that covers it.

Squonk
  • 48,735
  • 19
  • 103
  • 135
0

The Database entries will be there even after the App is updated (if user doesn't clear data). So in your situation I will:

  • check if there are is any database and its not empty.
  • if DB is there then read and create corresponding SharedPreferences.
  • delete the database using context.deleteDatabase(DATABASE_NAME);

I think that the Application class will be the best place to do so.

I am not sure if it is the best (or worst :P) method to do the same but I hope its useful.

SMR
  • 6,628
  • 2
  • 35
  • 56