Reputation: 11460
In Android, is it OK to read values that exist in the UI thread (e.g. an Activity subclass's member fields) from an AsyncTask.doInBackground()
, just not try to write them? Or should reading only be done in AsyncTask.onPostExecute()
?
Upvotes: 1
Views: 623
Reputation: 1007266
Ideally, you don't touch an activity within doInBackground()
of an AsyncTask
. The reason: configuration changes (e.g., screen rotations). The activity might be replaced while the thread is chugging along, and therefore you might wind up reading from the wrong activity.
If you follow the recipe I outline in this answer over yonder, and you synchronize access to the Activity
being held by the AsyncTask
, you'll get as good of results as possible. Of course, you will also have to deal with thread safety of whatever it is you're reading ("e.g. an Activity subclass's member fields"), as they may or may not be thread-safe already.
Upvotes: 2