0

What happens if some activity lifecycle events occur while a method is executing?
My activity has some methods where JSON text files (few tens of Kb) are written to disk, for example. Is it wrong designed? Or, in general, consider the case where there are two instructions to be executed: the first performs an important operation, the second updates some data based on the result from the first.

Something like:

value=performOperationAndYeldResult();
updateAppData(value);

What happens if the lifecycle event occurrs after the first call and before the second, or while it's executing?
I put "long" operations in services but I cannot create a service for every critical data update all over the app. I think I am missing something so I fear for nothing. Maybe the resumed or restarted activity continues operations so they get completed. Anyway I ask here: Are methods completed, or continued?

4

1 回答 1

0
new PerformOperationAsyncTask(someValue);

private class PerformOperationAsyncTask extends AsyncTask<Object, Void, Object> {

        protected Long doInBackground(Object... values) {

            //this is taking place in secondary thread          
             Object returnValue = performOperationAndYeldResult(values[0]);

             return returnValues;
         }

         protected void onPostExecute(Object result) {
             //this is taking place in the UI thread
             updateAppData(value);
         }
     }
于 2013-09-28T15:36:09.967 回答