Question

I am developing an android app and I need to save data when user is making a market update. Actually before the update is started. I tried using onDestroy() but that is not called when the app is updated. Do you have any ideas?

The problem is the app is saving data on a regular basis. And sometimes it happens that when you go and select update it tries to save on a file at the same exact time causing the data to get corrupted ... That's why I am trying to detect the update event.

File is saved at a fixed interval using a scheduled thread (e.g. 60 seconds). Also in the file I save a serialized Object using the classic writeObject(). This is happening only at a fixed rate of 60 seconds and also on the activity's onPause or service's onDestroy().

However if the fixed rate save happens exactly when updating (which causes the app to be destroyed) then the save is incomplete and the object get corrupted causing the next time the app is started to get an invalid object from the save file.

Was it helpful?

Solution

A general approach to save your data (from the Android developpers documentation) on android is to either use:

  • a key-value pairs on the shared preferences
  • saving the data on a files
  • or using a SQLite dabtase

You should use those even during the regular activity lifecycle and I dont see why they would not solve your persistence needs between updates as well.

In order to avoid corruption, use SQLite Transaction if you are using SQL and check this question for corruption safe strategies when dealing with files.

OTHER TIPS

AFAIK there's no way to know when your app is going to be updated (you don't receive the package-related intents, because your app is not installed anymore during the update). It will simply be stopped as usual, all broadcast receivers unregistered, and updated.

What you can do, however, is add checks in your app so that when it starts it checks whether it was just updated and do whatever it is you have to do if it was. This is really simple, just store the current version of the app (which you can get via the PackageManager) in a shared preference, for example, and check the stored version against the current version every time the app starts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top