android - Use the same AsyncTask to update different views -
i have button on 6 different activities. clicking on button same task different params depending on activity.
this done using asynctask , in onpostexecute() button state changed.
somebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new task().execute("param1", "param2"); } } private class task extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { //background task using params[0], params[1] return "success" or "error"; } @override protected void onpostexecute(string result) { if (result == "success") { //change somebutton state }else{ //show error message } } instead of having same asynctask in 6 activities, how can use single asynctask activities , change respective view?
you should create task, methods onsuccess, onfailure , override them.
public class task extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { //background task using params[0], params[1] return "success" or "error"; } @override protected void onpostexecute(string result) { if (result == "success") { onsuccess(result); }else{ onfailure(result); } } protected void onsuccess(string result) {}; protected void onfailure(string result) {}; } and in activity use this:
somebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { new task(){ @override protected void onsuccess(string result){ // want } @override protected void onfailure(string result){ // want } }.execute("param1", "param2"); } }
Comments
Post a Comment