Run tasks on separate thread, UI thread
//when updating UI within "doInBackground" method, use runOnUIThread
runOnUiThread(new Runnable() {
@Override
public void run() {
//perform action
}
});
//enter command into main thread, can pass parameter in "execute" method
Object object = new Object();
new JSONTask().execute(object);
//to reference Object parameter, call objects[0] in "doInBackground"
public class JSONTask extends AsyncTask <Object, String, String> {
Object mainObject;
@Override
protected void onPreExecute() {
//perform action
}
@Override
protected String doInBackground(Object... objects) {
mainObject = objects[0];
//works because reference semantics
String data = "";
return data;
}
@Override
protected void onPostExecute(String result) {
//modify mainObject and perform action on result that doInBackground returns
}
}
//calling .get() method on "new JSONTask().execute()" will freeze UI thread