Android - Next Action After Asynctask Completes

I am trying to create a simple application that POSTS to a PHP server. I would like to create one class that can handle all post requests and then feed back the data back to the original activity.

I currently have a class called ConectionHandler that is designed to handle all Post requests.

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

public ConnectionHandler(ArrayList targetList, ArrayList dataList, String hostLocation){
    ...
}

protected String doInBackground(String... params){
    ...
    return returnData;
}

      

I am creating a ConnectionHandler object from my loginActivity. Once I get the data in the doInBackground method, I want it back to loginActivity so that I can process it and start a new activity.

Is there an easy way to do this? I've experimented with many different ideas and none are working yet.

+3


source to share


1 answer


Have you tried using the onPostExecute method in AsyncTask? When you return a value from doInBackground, it is sent to onPostExecute, which runs on the main UI thread.



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

    public ConnectionHandler(ArrayList targetList, ArrayList dataList, String hostLocation){
        ...
    }

    protected String doInBackground(String... params){
        ...
        return returnData;
    }

    @Override
    protected void onPostExecute(String result) {
       //do whatever you want with the data here 
    }    
}

      

+2


source







All Articles