I have this code running in my onDestroy function:
@Override
protected void onDestroy() {
if (!(null == theUser.glideId)) {
JSONObject req = new JSONObject();
try {
req.put("actionKey", "UserPresenceInactive");
req.put("userId", theUser.userId);
new ServerRequest().execute(req); //Run an AsyncTask
} catch (JSONException e) {
e.printStackTrace();
}
}
super.onDestroy();
}
In the AsyncTask I send a request to a server (that comes back with just a 200 response).
My question is, what are the implications (if any) for doing this?
Does the Activity get destroyed? Does the app stay awake and might go into ANR if the server doesn’t respond? any thoughts?
edit
I tried doing this instead but got a android.os.NetworkOnMainThreadException.
new Runnable() {
@Override
public void run() {
JSONObject req = new JSONObject();
try {
req.put("actionKey", "UserPresenceInactive");
req.put("userId", theUser.userId);
new ServerRequest().execute(req); //Run an AsyncTask
} catch (JSONException e) {
e.printStackTrace();
}
}
}.run();
UPDATE #2
using a Thread instead of a Runnable did the trick!
Yes the
Activitygets destroyed but theAsyncTaskis doing its job(working in background). There won’t be anyANRbecause you are not doing any background stuff on the UI. If you are updating any view on your UI afterAsyncTaskis completed then there would aNULLPOINTER ExceptionAFAIK. But, this would not be a good idea to run AsyncTask inonDestroy().