Skip to content

Instantly share code, notes, and snippets.

@ed-george
Created July 5, 2014 09:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ed-george/8097940a2b2a55d036c5 to your computer and use it in GitHub Desktop.
Save ed-george/8097940a2b2a55d036c5 to your computer and use it in GitHub Desktop.
Async Callback Example
/**
* @author edgeorge
*
*/
public interface AsyncResponse {
void onProcessFinish(String result, int id);
}
import android.os.AsyncTask;
public class ExampleAsync extends AsyncTask<String, Void, String> {
private int id;
public AsyncResponse delegate = null;
public ExampleAsync(int id) {
this.id = id;
}
@Override
protected String doInBackground(String... params) {
//do work here
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//call delegate
delegate.onProcessFinish(result, id);
}
}
public class Test extends Activity implements AsyncResponse{
private static final int ASYNC_ONE = 1; //ID for async
@Override
protected void onCreate(Bundle arg0) {
//other activity stuff before this
ExampleAsync async = new ExampleAsync(ASYNC_ONE);
async.delegate = this;
async.execute();
}
@Override
public void onProcessFinish(String result, int id) {
if(id == ASYNC_ONE){
// process result
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment