user2779311
user2779311

Reputation: 1874

How can i know when the async task has returned

I have an async task that loads image urls from server.After loading urls than i load the images one by one through another asynctask. On the click of a button i start the first asynctask

    public void getit(View v)
    {

        new getdata().execute("http://10.0.2.2/geturls.php");
// line no 2
    }

After i get the urls i use another async task to load images. How can i find out when the image urls have been loaded and i can call the second async task at line no 2. if i use a boolean variable which i toggle in the onpostexecute

 @Override
            protected void onPostExecute() {
urlgot=true;
}

then i shall have to use some repeating loop inside getit method at line no 2 to check the status of this variable urlgot. but it may take more time than allowed for ui thread. Can there be a more cleaner method to do this check. thanks

Upvotes: 0

Views: 72

Answers (5)

I use a custom interface to do stuff after execution.

public Interface OnDataReceived
{
    public void onReceive( Object result);
}

and on MyASyncTask

public class MyAsyncTask extends ASyncTask<Object,Object,Object>
{
    OnDataReceived receiver;
    public MyAsyncTask( OnDataReceived receiver )
    {
        this.receiver  = receiver;
    }
...
    protected void onPostExecute( Object result)
    {
        receiver.onreceive( result );
    }
}

and let my main class implement OnDataReceived

public class Main implements OnDataReceived 
{
....
    public void getit(View v)
    {
        new MyAsyncTask(this).execute("http://10.0.2.2/geturls.php");
    }
    @override
    public void onReceive( Object result)
    {
        // do whatever
    }
}

EDIT

Even for more control you can add onFailed and rename your interface to OnResponse

public Interface OnResponse
{
    public void onReceive( Object result);
    public void onFailed( Object errcode);
}

Upvotes: 0

Rudi
Rudi

Reputation: 4314

instead of putting line 2 in getIt, put it in onPostExecute like below :

public void getit(View v)

    {
        new getdata().execute("http://10.0.2.2/geturls.php");
    }



   @Override
    protected void onPostExecute() {
      // line 2 
   }

Upvotes: 0

leandrocastelli
leandrocastelli

Reputation: 546

Use a Handler. In the method onPostExecute of your AsyncTask you can send a message informing the Handler to start another AsyncTask.

Something like this:

@Override
    protected void onPostExecute(Void res) {            

        MyHandlerHandler handler = new MyHandlerHandler();
        Message msg = new Message();
        msg.what = MyHandler.TASK_FINISHED;
        handler.sendMessage(msg);
    }

And in your Handler class:

public class MyHandlerHandler extends Handler {
            public static final int TASK_FINISHED = 2;
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {

        case TASK_FINISHED:
            new MyAsyncTask().execute();
            break;
        }
    }
}

Upvotes: 0

Jeffrey Klardie
Jeffrey Klardie

Reputation: 3028

There are two solutions I can think of:

1) You create one AsyncTask that does everything (getting the urls, and downloading all images). Than you know exactly when to start downloading the images.

2) You start the next AsyncTask from the onPostExecute() of the first AsyncTask.

Upvotes: 2

Tenfour04
Tenfour04

Reputation: 93609

You won't be able to do your next piece of work in //line no 2 without defeating the purpose of AsyncTask. If you're doing network activity, you need to be doing it asynchronously, so that's not an option.

Instead, in onPostExecute() you can call another method in your activity that does what you would have done in //line no 2. This is safe to do, because onPostExecute() happens on the UI thread.

But depending on your design, it might make more sense to do all the //line no 2 stuff in your original AysncTask in onPostExecute, so you only have one task doing all of the work.

Upvotes: 0

Related Questions