Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to use the same AsyncTask in different places on my App and I want a different onPostExecute for each one. It's possible to call them from onCreate method instead of from AsyncTask class?

Something like:

new AsyncTCP.execute()
onPostExecute(){
//DO STUFF
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just create an AsyncTask with no onPostExecute method overridden

public class YourTask extends AsyncTask<Whatever, Whatever, Whatever> {

    @Override
    protected Whatever doInBackground(Whatever... whatever) {
        // whatever
        return whatever;
    }

}

And then when you want different implementations of onPostExecute, create anonymous task or descendant class and use it

task = new YourTask() {

    @Override
    protected void onPostExcecute(Whatever result) {
        //whatever
    }

};
task.execute();

Or better

public class YourTaskAnother extends YourTask {

    // just override onPostExecute here

    @Override
    protected void onPostExcecute(Whatever result) {
        //whatever
    }
}

task = new YourTaskAnother();
task.execute();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...