4

I have an application in android, where I am accessing couple of REST webservices. I am using AsyncTasks to access these and do some UI changes afterwords. I would like to use some of these calls in different activities. However, according to all tutorials I've read, AsyncTasks are used as internal classes in the activities (which makes sense, because they are changing the views in those activities). But how can I solve the reusability of those AsyncTasks? Even more, let's say I want to do some AsyncTasks calls upon location change. I have a LocationListener (which is a single external class) and I would like to do the API call inside of the onLocationChange() method. But I cannot access the views I would like to, because I am not in the activity class. Does anybody have a nice solution or idea how to do the architecture of the classes? Thank you.

Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
  • 1
    From OO perspective, you should create reusable a business component, not a reusable AsyncTask. Check out my answer [here](http://stackoverflow.com/questions/8295003/best-way-to-manage-the-progressdialog-from-asynctask/8317071#8317071) for more details. – yorkw Apr 24 '12 at 23:27
  • You're right, however, it seems like repeating a lot of code, when the only thing I need is different implementation of the onPostExecute() method in different activities. – Filip Majernik Apr 25 '12 at 08:01
  • IMO it is much better that isolating AsyncTask, you are not gain any real benefit (reusability, testability), rather than adding coding complexity (context reference passing in and out). When talking about code refactoring from OO perspective, think more from problem abstraction level, not simply strip off inner class (AsyncTask, BaseAdapter and etc.) from purely code level. If you really want simpify your code, consider using super Activity maintain AsyncTask code, which is better than create AsyncTask as separate class. – yorkw Apr 25 '12 at 10:35

1 Answers1

4

You can create a Async task as a seperate class instead of integrating within any activity.

sample code is below:::

    public class KeyTask extends AsyncTask<Void, Void, String> {

    @Override 
    protected void onPreExecute() { 
        Log.i("onPreExecute", ".onPreExecute() — start");


    } 

    @Override 
    protected String doInBackground(Void ... params) {
        Log.i("TransmissionKeyTask", ".doInBackground() — start");

        }

    @Override
    protected void onPostExecute(String result) {
        Log.i("onPostExecute", ".onPostExecute() — start");
    }       
}

and even you add constructor in it like below::

public KeyTask(Context context) {
        // TODO Auto-generated constructor stub

}
W I Z A R D
  • 1,224
  • 3
  • 17
  • 44
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • 1
    Yes, that could be done, but if I need to change some components I have to do it in the onPostExecute method. And if the task is used in more different activities, that could be a problem. I think that could be done by checking the instance of the context (am I right?) In that case, how could check if the user hasn't already navigated to a different activity? – Filip Majernik Apr 24 '12 at 17:07
  • 1
    you can use either enumaration or switch case and many more to differentiate just while calling these task have a contructor and call using the specific value to differentiate – Shankar Agarwal Apr 24 '12 at 17:10