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 Activity
gets destroyed but the AsyncTask
is doing its job(working in background). There won't be any ANR
because you are not doing any background stuff on the UI. If you are updating any view on your UI after AsyncTask
is completed then there would a NULLPOINTER Exception
AFAIK. But, this would not be a good idea to run AsyncTask in onDestroy()
.