开发者

Why publishing an upgrade reinstalls the application deleting the internal data storage?

开发者 https://www.devze.com 2023-02-17 04:10 出处:网络
I developed and application and then I published it on Android Market. After developing several improvements I generated an upgrade for the application.

I developed and application and then I published it on Android Market. After developing several improvements I generated an upgrade for the application.

The upgrade has been successfully published on Android Market, and devices got updated. Nevertheless, the application hasn't been upgraded on user's devices; it has been removed and installed again, losing the data the application stored on the internal storage.

How can I publish an ap开发者_运维知识库plication upgrade that will be installed on the user's devices keeping the application's data? How are the application's upgrades being published for just upgrading the application without reinstalling it?

My application runs from Android 2.0 (API 5).


If you have a data provider and you followed one of the examples out there, there's probably a DATABASE_VERSION in your code. Alongside that, there's probably something that looks like this:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    LogUtils.w(mContext, "Processor", "Upgrading database from version " + oldVersion + " to "
        + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS "+REST_TABLE_NAME);
    onCreate(db);
}

That means that if you change the DATABASE_VERSION when you update your code, it will drop the tables and re-create them from scratch. If that's what is going on, you have two choices:

1) Don't change the DATABASE_VERSION. Of course, this only makes sense if you didn't change your schema, but there you go.

2) Write migration code for onUpgrade. You can check the old version of the database and write code to migrate the data to your new schema. This can be difficult, but if your app has considerable user-generated data, can be worthwhile.

EDIT: Just to be clear, your application isn't being re-installed in this case, but because your database is re-created, data is being destroyed, so it might appear like it is being re-installed. Your shared preferences, though, should be untouched on an upgrade.

0

精彩评论

暂无评论...
验证码 换一张
取 消