Archie.bpgc
Archie.bpgc

Reputation: 24012

Use the same AsyncTask to update different views

I have a button on 6 different Activities. Clicking on that button does almost the same task with different params depending on the Activity.

This will be done using an AsyncTask and in onPostExecute() the button state will be changed.

someButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new Task().execute("param1", "param2");
    }
}

private class Task extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        //background task using params[0], params[1]
        return "success" or "error";
    }

    @Override
    protected void onPostExecute(String result) {

        if (result == "success") {
            //change the someButton state
        }else{
            //show an error message
        }
}

Instead of having the same AsyncTask in all the 6 Activities, how can I use a single Asynctask from all the Activities and change the respective view?

Upvotes: 1

Views: 284

Answers (2)

You should create Task, with methods onSuccess, onFailure and override them.

public class Task extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        //background task using params[0], params[1]
        return "success" or "error";
    }

    @Override
    protected void onPostExecute(String result) {

        if (result == "success") {
            onSuccess(result);
        }else{
            onFailure(result);
        }
    }

    protected void onSuccess(String result) {};
    protected void onFailure(String result) {};
}

and then in activity use it like this:

someButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new Task(){
            @Override
            protected void onSuccess(String result){
                // do what you want
            }
            @Override
            protected void onFailure(String result){
                // do what you want
            }
        }.execute("param1", "param2");
    }
}

Upvotes: 1

Thibault D.
Thibault D.

Reputation: 10005

Put your Task in its own file and make it public.

Create a callback interface:

public interface TaskCallback {
    public void onSuccess(String result);
    public void onFailure(String errorMessage);
}

Give such a callback to your Task:

public class Task extends AsyncTask<String, Void, String> {
    private TaskCallback callback; 

    public Task(TaskCallback callback) {
        this.callback = callback;
    }

    @Override
    protected String doInBackground(String... params) {

        //background task using params[0], params[1]
        return "success" or "error";
    }

    @Override
    protected void onPostExecute(String result) {
        if (result == "success") {
            callback.onSuccess(result);
        } else{
            callback.onFailure(errorMessage);
        }
    }
}

And then implement the callback when creating the Task instance in your activity:

someButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        private TaskCallback callback = new TaskCallback() {
            @Override
            public void onSuccess(String result) {
                //change the someButton state
            }
            @Override
            public void onFailure(String errorMessage) {
                //show an error message
            }
        }
        new Task(callback).execute("param1", "param2");
    }
}

Upvotes: 1

Related Questions