I am aware that there are multiple approaches on how to return some data from AsyncTask
to the calling Activity
. I can use Message
object from the Handler
or I can use Interface-based
approach with both Activity
and my AsyncTask
implementing the same interface (with method that passes the data).
And then again there are other approaches like with the public
method in the calling Activity
and passing that Activity
to the AsyncTask
and then filling Activity's
field list with the result data. Or overriding onPostExecute
from the calling Activity
, and so on, and so on.
Now, in terms of memory consumption and speed of execution, which approach is the BEST to use?
NOTE: If we take the rating of SO experts, I saw that guys with 20k+ rating suggest either Message
or Interface-based
approaches. Some less experienced, but still with 10k+ credit, suggest other solutions like the 3rd one I mentioned. That is the root of my confusion because no one of them mentioned anything about how "heavy" their solution is.
Passing activity is not a good approach. Because that is a burden on memory.(All the fields are loaded in memory from previous activity). Also it makes the 2 activities tightly bound.
The first 2 are the correct approach depending upon the scenario.
Message based communication consumes a lot of android OS resource. As for every message received the OS looks for all the handlers associated with it. It is used mostly when your activity has to communicate to various other activities also activities external to your application.
Interface based / Callback method is more preferential in your case . If your activity has to communicate to fewer activities and within your application. It would consume lesser memory and and computation overhead from android OS