In Android, I'm having trouble figuring out how, when an AsyncTask is complete, to notify a Fragment that is listening. It seems that the usual Java stuff, like say PropertyChangeLIstener
is not available. So in onPostExecute
, I want to fire an event and have OtherFragment
see that.
How do I do this?
public class MainActivity extends FragmentActivity {
<stuff>
private class ConsumeWebService extends AsyncTask
<String, // type of the parameters sent to the task upon execution - doInBackground()
Integer, // type of the progress units published during the background computation -onProgressUpdate()
String> {
public ConsumeWebService(SomeKindOfListener listener){
this.myListener = listener;
}
protected String doInBackground(String... urls) {
< get JSON data from RESTful service>
< create and populate SQLite db wit JSON data>
return jsonData;
}
@Override
protected void onPostExecute(String result) {
try {
<fire some kind of event>
}
catch (Exception e) {
}
}
}
}
public class OtherFragment extends Fragment implements SomeKindOfListener {
@Override
public void someKindOfChange(SomeKindOfChangeEvent arg0) {
< do the stuff we want to do>
}
}
public interface SomeKindOfListener {
void onPostExecuteDone();
}