0

I have 3 activities A,B,C. In all the 3 activities i'm using Async task. Is it possible to run all the Async task under a single Async task(Common code).

If possible
1. How to check which task called from which activity?
2. How to check whether the task got completed or not?

Vignesh
  • 2,295
  • 7
  • 33
  • 41
  • Do A, B and C run the same operation in their respective AsyncTasks? Or are they doing different things in the background? – baske Mar 13 '13 at 10:23
  • they are doing different – Vignesh Mar 13 '13 at 11:25
  • If that is the case then I would say that you actually wouldn't even want to run all 3 AsyncTasks in one.. You would either be doing too much work (e.g. downloading something for activity B when it is never showed to the user), or doing work in the wrong order (e.g. downloading for A first, B second and C third) when the user is viewing C. Putting common code into a generic super class is probably in that case also not worth the effort (AsyncTasks being pretty simple building blocks already). – baske Mar 13 '13 at 15:53

3 Answers3

1

May be you want to have a Common async task for that can used to perform long running taks and you want a callback machanism to it use this, You can implement the same by taking async task class a separate abstract and by implementing a callback interface. Async Class with callback

Community
  • 1
  • 1
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • This is exactly why I asked the question in my comment above.. If all those Activities need to run the same operations in the background (like, query some ContentProvider) an AsyncTask with callback would be the way I would implement it as well. – baske Mar 13 '13 at 10:26
  • @baske your answer http://www.stackoverflow.com/questions/13966343/progress-dialog-is-not-displaying-in-async-task-when-asynctask-is-called-from-se in this question also helped in understand when I am using async task for first time – Pragnani Mar 13 '13 at 10:48
0

Yes it is possible.

  1. Add a Parameter that is used to indicate the calling Activity
  2. Look at JavaDoc of AsyncTask method onPostExecute()
Dodge
  • 8,047
  • 2
  • 30
  • 45
  • I think passing in a param to indicate the calling Activity is less elegant than providing the callback mechanism described in the answer of Pragnani. – baske Mar 13 '13 at 10:28
0

Create your AsyncTask class

public class MyTask extends AsyncTask<Void, Void, Void>
{

    // Use a WeakReference instead of holding the Activity object
    private WeakReference<Activity> mActivity;

    public MyTask(Activity activity)
    {
        mActivity = new WeakReference<Activity>(activity);
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        // do common work
        return null;
    }

    public Activity getActivity()
    {
        return mActivity.get();
    }

    public void setActivity(Activity activity)
    {
        mActivity = new WeakReference<Activity>(activity);
    }

}

And in each Activity:

MyTask t = new MyTask(YourActivity.this)
    {
        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            // do some work when finished           
        }
    };
Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61