Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

android - AsyncTask onPostExecute call from outside the AsyncTask

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

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

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...