Fredisson
Fredisson

Reputation: 25

How I can do something after AsyncTask finish

When AsyncTask finished to work, I want to call a method onResume(). AsyncTask is not embedded in my activity - they are separate classes, so I can't call a method onResume() within a method onPostExecute().

How to wait for the end of the AsyncTask to call a method onResume()?

Thank you very much for your answers, I solved the problem.

Upvotes: 0

Views: 2601

Answers (2)

Amar1989
Amar1989

Reputation: 512

you can use interface that you can implement on post execute

example:

public class DownloadFileUsingAsync  extends AsyncTask<String, String,  String> {


public interface OnTaskCompleted {
    void onTaskCompleted(String str);
    void onTaskFailure(String str);
}
private OnTaskCompleted listener;

public DownloadFileUsingAsync(OnTaskCompleted listener,File folderPath,String fileName,
        String data, String method,
        int timeout) {
    this.listener = listener;

}

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

@Override
protected void onPostExecute(String fileUrl) {
    if (listener != null) {
        listener.onTaskCompleted(fileUrl);
    }

}

@Override
protected void onPreExecute() {
}

protected void onProgressUpdate(Void... values) {
}

}

calling aynctask

     new DownloadFileUsingAsync(listener,folder,docname, null, "GET",
            10000).execute(docUrl);

and on post execute you can handle listener like this:

   private OnTaskCompleted listener = new OnTaskCompleted() {
    //write here 

    }
    /**
     * onTaskFailure method for load base image failure callback.
     *
     */
    public void onTaskFailure(final String error) {
        //handle error

    }
};

Upvotes: 1

Renan Ferreira
Renan Ferreira

Reputation: 2150

I think you should take a look at the meaning of the method onResume().

http://developer.android.com/training/basics/activity-lifecycle/starting.html

This method is invoked by the lifecycle of the Activities, so you shouldn't make a call to this method in your code. Just override their comportament if you need to make some logic in this point of the lifecycle of the activity.

Nevertheless, if you want to call an Activity method in the class that you embedded your AsyncTask, you can pass the context or the full activity as a parameter of the constructor of this class. Take a look at this answer:

How to call parent activity function from ASyncTask?

Upvotes: 1

Related Questions