Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async task Android execute

Tags:

People also ask

How do I run Async tasks on Android?

To start an AsyncTask the following snippet must be present in the MainActivity class : MyTask myTask = new MyTask(); myTask. execute(); In the above snippet we've used a sample classname that extends AsyncTask and execute method is used to start the background thread.

Why AsyncTask is used in Android?

Android App Development for Beginners Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.


This was asked in one of the Android interviews. I was asked whether it's possible to start another async task (let it be Task2) from doInBackground() method of async task 1(let it be Task1). I had gone through the docs which say the following:

The task instance must be created on the UI thread.

execute(Params...) must be invoked on the UI thread.

As per these statements, I think that it shouldn't be possible to start a task from background method of another task. Also, async task has UI methods (which cannot be used on a background thread), so that strengthened my argument and I answered it as not possible.

On checking on a simple demo app, I saw that it's indeed possible to do so. Some demo code:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        init();
        Log.v ("gaurav", "Thread is : " + Thread.currentThread().getName());
        Task1 task = new Task1();
        task.execute();
    }

class Task1 extends AsyncTask {
    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        Log.v ("gaurav", "Thread task 1 is : " + Thread.currentThread().getName());

        Task2 task = new Task2();

        task.execute();
        return null;
    }
}

class Task2 extends AsyncTask {
    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        Log.v ("gaurav", "Thread task 2 is : " + Thread.currentThread().getName());

        Log.v ("gaurav", "Task 2 started");
        return null;
    }
}

I get following logs indicating successful execution :

> 08-07 09:46:25.564: V/gaurav(2100): Thread is : main 08-07
> 09:46:25.564: V/gaurav(2100): Thread task 1 is : AsyncTask #3 08-07
> 09:46:25.564: V/gaurav(2100): Thread task 2 is : AsyncTask #4 08-07
> 09:46:25.564: V/gaurav(2100): Task 2 started

I have checked this on ICS, KK and L device and it works fine for all.

One reason I could think of is that I'm not overriding any UI methods and doing any UI updation in my second task, hence it doesn't cause any problems, but I'm not sure. Even if that's the case, it violates the threading rules mentioned in the developer guide.

As a reference, I checked out this link too : Start AsyncTask from another AsyncTask doInBackground() but the answer states to start the second task using runOnUiThread() method inside doInBackground(). I'd like some help on what's going on here. Thanks.