How to get return value from AsyncTask to main activity?

I have a class (for example, a helper class) - this is not an activity that manages soap requests. I use this class to send soap requests that come from actions using the doInBAckground method and catch all return values ​​from webservice by onpostexecute. Everything is fine, but my problem starts from now, because I could not pass the asynctask class of the return value to the main class.

+4
source share
2 answers

You may have some utility class available as singleton (ok, singleton is a dangerous template but this use is justified in android until we get a reasonable and isabile usabel injection) and pass the result.

Advandatges: - do not mess with intentions / sequence - pass data or call some methods or do what you like - all your actions have the same singleton service instance.

Disadvantages: - a singleton pattern is considered dangerous

You can even go further and make your singleton service - you will start using it as an asynchronous task, and then your activity can request results using the selected methods.

Or you can take another step: register your activity as a listener in the asynchronous call service and call the method in this action when you are ready (note: since this will not be a UI thread, you cannot do something with the UI if you use runOnUiThread ()

+1
source

Have you tried to implement AsyncTask as an inner class of your activity?

+1
source

All Articles