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?
source share