android - Call AsyncTask from another class -
in existing app have activity inner class extends asynctask, looks following:
public class activity_1 extends baseactivity { .... new async().execute(); ... public class asyncextends asynctask<void, void, string> { protected string doinbackground(void... progress) { ... } protected void onpreexecute() { ... } protected void onpostexecute(string result) { ... } } } now, need call same doinbackground-method activity, onpostexecute() of inner class operates on local ui variables , hence it's not possible use outside clas. there way can call asynctask, , override onpostexecute andonpreexecute-method, or shall create yet inner-class in other activity, same background thing (of course move common utility-class or something), etc...?
you can make separate abstract package private class, extending asynctask , implementing doinbackground() method:
abstract class myasynctask extends asynctask<void, void, string> { @override final protected string doinbackground(void... progress) { // stuff, common both activities in here } } and in activities inherit myasynctask (new class should private, way), implementing onpostexecute() , onpreexecute() methods:
public class activity_1 extends baseactivity { ... new async1().execute(); ... private class async1 extends myasynctask { @override protected void onpreexecute(){ // activity 1 gui stuff } @override protected void onpostexecute(string result) { // activity 1 gui stuff } } } if onpreexecute , onpostexecute contain common actions well, can apply following pattern:
abstract class myasynctask extends asynctask<void, void, string> { public interface myasynctasklistener { void onpreexecuteconcluded(); void onpostexecuteconcluded(string result); } private myasynctasklistener mlistener; final public void setlistener(myasynctasklistener listener) { mlistener = listener; } @override final protected string doinbackground(void... progress) { // stuff, common both activities in here } @override final protected void onpreexecute() { // common stuff ... if (mlistener != null) mlistener.onpreexecuteconcluded(); } @override final protected void onpostexecute(string result) { // common stuff ... if (mlistener != null) mlistener.onpostexecuteconcluded(result); } } and use in activity following:
public class activity_1 extends baseactivity { ... myasynctask atask = new myasynctask(); atask.setlistener(new myasynctask.myasynctasklistener() { @override void onpreexecuteconcluded() { // gui stuff } @override void onpostexecuteconcluded(string result) { // gui stuff } }); atask.execute(); ... } you can have activity implement myasynctasklistener well:
public class activity_1 extends baseactivity implements myasynctask.myasynctasklistener { @override void onpreexecuteconcluded() { // gui stuff } @override void onpostexecuteconcluded(string result) { // gui stuff } ... myasynctask atask = new myasynctask(); atask.setlistener(this); atask.execute(); ... } i wrote code head, might contain errors, should illustrate idea.
Comments
Post a Comment