Hong Wei Wang
Hong Wei Wang

Reputation: 1398

Android Asynctask Generally Understanding Questions

Android Asynctask Generally Understanding Questions.

  1. If I want to make Asynctask SyncTask, how do I do that?

    AsyncTask A = new AsyncTask(); AsyncTask B = new AsyncTask(); A.execute(); B.execute();

If I want A to finish before B starts how should I do that?

  1. If I close an Activity, does the AsyncTask call on that activity gets destroy?
  2. If I close the whole application, does the AsyncTask call on that application gets destroy?

Upvotes: 0

Views: 101

Answers (3)

Naveed
Naveed

Reputation: 3212

AsyncTask is an abstract class so you must extend it in order to add your app specific functionality to it. You implement the doInBackground() method to do what ever work is required. The AsyncTask documentation explains it in detail. I will give a brief answer to each of your question.

If I want to make Asynctask SyncTask, how do I do that?

You have the right idea with creating the async task, however as I mentioned before you have to subclass the async task to actually do some work.
Here is an example (note that the Void types do have meaning however the documentation covers them in great detail)

   public class MyTask extends AsyncTask<Void, Void, Void>
    {
        //This method will run on the back ground thread
        @Override
        protected Void doInBackground(Void... params)
        {

           // All the heavy work should be done here e.g. 
           // loading from network, database etc.
            return null; 
        }
    }

Then in your activity you would create and run this task as follows :

  MyTask myTask = new MyTask();
  myTask.execute()

If I want A to finish before B starts how should I do that?

As the documentation states:

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

Which means if you are using honeycomb or later async tasks will run on a single thread. So normally A should get executed before B if you execute in that order. But I would do something like this: Launch A, then when onPostExecute() of A gets called, you know that A is done, you can start your task B from this method to be sure that A finishes before B.

If I close an Activity, does the AsyncTask call on that activity gets destroy?

The short answer to this question is No. The async task will continue to run even if the activity has called on destroy. However if the async task is tied to the activity life cycle you need to make sure you end the task when the activity dies or you will run into problems.

If I close the whole application, does the AsyncTask call on that application gets destroy?

I am not 100% sure about this one. I believe the behavior is unspecified since now its up to Android to collect resources. It may decide to kill the task right away if its low on resources or it may not. However you should avoid this design. If you need to run something after your application has closed, then look at the Service class.

Upvotes: 0

Yogesh Lakhotia
Yogesh Lakhotia

Reputation: 888

call b.execute() in onPostExecute() of A

Upvotes: 1

Related Questions