let all your activities implements
an interface say Ixxx, that has a method, activityOnPostExecute(String json)
, and pass the caller activity at the asyncTask
constructor as type Ixxx
, GenerucTask(Ixxx caller)
, and at postExecute()
of the GenerucTask
invoke caller.activityOnPostExecute(result);
now at every activity override the method activityOnPostExecute()
.
EDIT:
here is waht i mean
1- Create the interface
that all activities should implement, with the onPoseExec()
method:
public interface IAsyncHandler {
public void onPostExec(String json);
}
2- Create the GenericAsyncTask class,
public class GenericAsyncTask extends AsyncTask<String, Integer, String> {
IAsyncHandler handler = null;
public GenericAsyncTask(IAsyncHandler h){
handler = h;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
//whatever work goes here ...
//return str as a result
return str;
}
@Override
protected void onPostExecute(String result) {
//call the caller's class onPostExec(), it should be ovedrridden in the activity
handler.onPostExec(result);
}
}//CLASS
3- At your activity, override the method onPostExec()
@Override
public void onPostExec(String json) {
// TODO Auto-generated method stub
}
4- At your activity onClick()
, or somewhere execute the GenericAsyncTask
onClick(View v){
GenericAsyncTask task = new GenericAsyncTask(this);
task.execute("some", "params","...");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…