0

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.

Fredisson
  • 25
  • 1
  • 7
  • Why don't you pass a reference to the activity in the constructor so you can invoke any method of it when task is done? – Juanjo Vega Feb 13 '15 at 10:26

2 Answers2

1

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?

Community
  • 1
  • 1
Renan Ferreira
  • 2,122
  • 3
  • 21
  • 30
  • Thank you, I know the life cycle of activity. But your method does not solve the problem - and so I sent the context in the constructor AsuncTask, but you can not call onCreate within AsyncTask. It must somehow make the main stream. – Fredisson Feb 13 '15 at 10:48
  • What's de meaning of calling onCreate after a background job? You want to recreat your activity? – Renan Ferreira Feb 13 '15 at 11:14
1

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

    }
};
Amar1989
  • 502
  • 1
  • 3
  • 17