2

I have developed a class that manages all my API calls to my server (via AsyncTask) problem is, that I want after the doInBackground(), in the onPostExecute() to pass externally a callback function that will be executed in the onPostExecute().

In that way, I can keep my communication class generic, and the Activity will send it a callback to activate and update the UI.

Any idea how do I do that?

thanks!

Ahmad
  • 69,608
  • 17
  • 111
  • 137
Himberjack
  • 5,682
  • 18
  • 71
  • 115
  • 1
    http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask/16752189#16752189. check the answer by blackbelt. looking for something similar? – Raghunandan May 26 '13 at 20:10

2 Answers2

3

although answer is accepted, just adding another, real anonymous implementation. hope this help others.

your interface : you can implement this inside AsyncTask class.

public interface ImageLoaderListener{
    void imageDownloaderCallBack(Bitmap bmp);
}

AsyncTask class Constructor :

// declare interface variable
private ImageLoaderListener listener;
private String link;

public ImageDownloader(String link, ImageLoaderListener listener){
    this.link = link;
    this.listener = listener;
}

onPostExecution :

@Override
protected void onPostExecute(Void result){
    listener.imageDownloaderCallBack(bitmap);
    // your code, i was returning bitmap
}

implementation in Activity class :

ImageDownloader imageDownloader = new ImageDownloader(url, new ImageLoaderListener(){
     @Override
     public void imageDownloaderCallBack(Bitmap bmp) {
            // update Ui elements
     }
});
imageDownloader.execute();

Also, you should remember that if any Ui elements need to be updated based on imageDownloaderCallBack return values, you should write that code inside function itself.

Max
  • 1,528
  • 21
  • 33
2

Here you go mate:

public class ApiMethods {

public interface OnCommandFinished {
    public void onApiSuccess(String result);
    public void onApiError(String error);
}

public void like(PARAMS .... , OnCommandFinished respondTo){
    new runRequestTask(respondTo).execute(uri, params);
}

private class runRequestTask extends AsyncTask<Object, Void, String>{

    private final OnAtomicCommandFinished mRespondTo;

    public runRequestTask(OnCommandFinished respondTo){
        mRespondTo = respondTo;
    }

    @Override
    protected void onPostExecute(String result) {
        // IF SUCCESS   
        mRespondTo.onAtomicSuccess(result);

        // IF ERROR
        mRespondTo.onApiError("404....");
    }
}

}

To run the code, you simply call like(...) with a class that implements the OnCommandFinished

Sean
  • 5,176
  • 2
  • 34
  • 50
  • What would you pass into `like(...)` for the `respondTo` parameter? – Mike Richards Jul 24 '14 at 13:32
  • 1
    The `respondTo` is simply the interface which expected the result, the caller. The Caller started this thing by calling the `like` method with some parameters, which really depends on your implementation. – Sean Jul 26 '14 at 18:17