0

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.

Slacky
  • 137
  • 8
  • I don't understand your question. Do you mean that you need to save data when your applicaton is minimized? or when the user updates the application to a newer version? – Stephan Celis Jan 31 '13 at 09:40
  • onDestroy is called when a Activity is not being used and Android needs to free up some memory. It has nothing to do with updating an application in android market. – Stephan Celis Jan 31 '13 at 09:40
  • see http://stackoverflow.com/questions/13263855/how-to-retain-the-global-variebles-when-application-is-upgraded-to-new-version-i/13263998#13263998 will sure help you. – Chintan Khetiya Jan 31 '13 at 09:49
  • 1
    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. – Slacky Jan 31 '13 at 09:57

2 Answers2

3

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.

Community
  • 1
  • 1
Thomas
  • 2,751
  • 5
  • 31
  • 52
  • I think I didn't make it clear enough. I know about those methods of saving data. I am using the data on files one. 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. – Slacky Jan 31 '13 at 09:56
  • Can you then perhaps modify the question to include how you are saving your data on the close/destroy and what you are doing on the onstart? I am wondering if there is not a problem on the way you are handling the file on those – Thomas Jan 31 '13 at 10:08
  • 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. – Slacky Jan 31 '13 at 10:24
  • Also about SQLite... is it safe? I mean if you have a rooted phone can you get access to any database ? – Slacky Jan 31 '13 at 11:28
  • Regarding SQLite, it is not safe (see http://stackoverflow.com/questions/3608883/how-secure-are-sqlite-and-sharedpreferences-files-on-android) on the other hand, it should be possible to avoid corruptions (due to being killed) by using SQLite Transaction. On files, there is a bit of a discussion on this question http://stackoverflow.com/questions/10247118/avoiding-data-loss-due-to-interruption-when-saving-files-on-android where I guess the temp file solution could work for you, but may not be much efficient – Thomas Jan 31 '13 at 11:49
  • 1
    If you have a rooted phone, you can get access to anything. SQLite databases, private preferences & files, anything. If you have **really** private data (e.g. passwords), you should encrypt it no matter where you save it. – Felix Jan 31 '13 at 12:03
1

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.

Felix
  • 88,392
  • 43
  • 149
  • 167
  • I don't have problems with that. The problem is WHEN an update is actually triggered. Please check my post as I explained my problem in greater detail – Slacky Jan 31 '13 at 10:32
  • Oh, OK, I see where your problem is now. In that case, @Thomas's answer is the correct one. Just make sure you finish writing your data and close all open files / databases / preferences objects in your components' `onPause` methods. – Felix Jan 31 '13 at 12:11