How to ensure that data is saved before android destroys the process

On Android, it is usually best not to perform database operations (or at least complex ones) in UI-Thread. I have an activity with a complex form, and I want all the data to be saved when the activity goes in the background (for example, the user presses the home button or a phone call arrives). In the actions onPause () - method, I can run AsyncTask, which stores the data in the database, but I can never be sure that the task completed successfully, because android can kill the process before the task is completed, because the activity and the entire application are in background mode.

I can save data synchronization in the onPause method, but then it can be run in ANR.
I know that Android restores views after activity has been killed, but this only works correctly when View Ids are unique. I have many programmatically added views where I cannot guarantee the uniqueness of identifiers and it is almost impossible to use the saveInstanceState function because I need to save very complex models.

Is it possible to guarantee that the data will be saved before the android destroys the process without doing this in UI-Thread?

+4
source share
4 answers

I created the application once when I had similar data consistency issues. What I did there was to delegate the storage of data objects in the Service, which I created specifically for this purpose. Although this makes starting / stopping / initializing your activity a lot more difficult (after the activity is started again, you have to wait for the service to complete its previously saved save action), it was the only “Android” way I could think of this a problem.

+3
source

You can explore the possibility of using the service for this if you are afraid that the system will destroy your background processes before they are completed. It may be overkill, but it will certainly work as expected =) Just google “Android Service Tutorial” if you don’t know how to use them.

-Services will not be killed if you do not want to!

0
source

Indeed, if you use AsyncTask in onPause() , Android can kill your application process without waiting for the workflow to complete. But he will not kill the process if there is a working Service . Therefore, a good solution is to implement the database synchronization logic using IntentService .

0
source

I have the same question when I need to save data: the user fills out a form or pauses the action. We must also take into account screen rotations or other events that could lead to data loss.

Here is what I found on the Android developer site :

For content provider data, we suggest that the actions use an “edit in place" user model. That is, any changes made by the user immediately without requiring additional confirmation. Support for this model is usually a simple matter of the following two rules:

  • When you create a new document, the database database or database file for it is created immediately. For example, if the user decides to write a new email, a new entry for this email is created as soon as they begin to enter data, so if they switch to any other activity after this indicates that this email will now appear in the draft list.
  • When the onPause () method is called, it must commit support for the content provider or make any changes made by the user. This ensures that these changes will be noticed by any other activity that is triggered. You may want to capture your data even more aggressively at key moments during the life cycle of your activity: for example, before starting a new activity, before completing your own activity, when the user switches between input fields, etc.

This model is designed to prevent data loss when a user moves between actions, and allows the system to safely kill activity (because system resources are needed somewhere else) at any time after it is suspended. Note this means that the user, pressing BACK from your activity, does not mean "cancel" - this means that the action content is saved. The cancellation of changes in the action must be ensured through some other mechanism, such as an explicit "return" or "cancellation" option.

0
source

Source: https://habr.com/ru/post/1479711/


All Articles