0

I want to send logout information to server, when the app gets destroyed.

Here's the code:

@Override
protected void onDestroy() {
    try {
        Log.i("myApp", "Activity destroyed");
        SharedPreferences prefs1 = getSharedPreferences("com.my.app", Context.MODE_PRIVATE);
        Log.i("myApp", "step1");
        String response;
        Log.i("myApp", "step2");
        response = HttpPOSTer.logout(EventCodes.LOGOUT, LOGOUT_EVENTCODE, prefs.getString("sessionID", "null"));
        Log.i("myApp", "step3");
        if (response.equals("logout")) {
            Log.i("myApp", "logged out succesfully");
        } else {
            Log.i("myApp", "couldn't perform logout");
        }
        prefs1.edit().clear().commit();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    } 
    catch (ExecutionException e) {
        e.printStackTrace();
    }   
    super.onDestroy();  
}

But here's the log, when I close the app from home button's long click menu:

enter image description here

I can't even debug here. I place breakpoint, but it never gets fired while debugging.

Is there any reason, why AsyncTask doesn't get called from onDestroy()?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • If "step3" isn't logged, an exception must have occured prior to your logging statement. Please post the code of your HttpPOSTer and the full logcat output. – Wolfram Rittmeyer May 21 '13 at 11:29
  • 1
    put your super.onDestroy() after last catch and see it. – baldguy May 21 '13 at 11:29
  • @WolframRittmeyer, none exception is thrown. Posted the whole logcat, have a look please. I assure you, everything is ok with HttpPOSTer class. – azizbekian May 21 '13 at 11:37
  • @Rahil2952, I've tried that one too, that didn't work for me. – azizbekian May 21 '13 at 11:37
  • what is the value of response?Put log there and check its value. – baldguy May 21 '13 at 11:47
  • @Rahil2952, the point is, that response never gets value, in other words logoout() never gets called, and therefore step3 never get logged. The question is: why that asynctask doesn't throw any kind of exception? – azizbekian May 21 '13 at 11:49

1 Answers1

0

its not a good idea to use asyntask in onDestroy() instead you can have an activity that extends IntentService and give a call to that activity from onDestroy

prvn
  • 916
  • 5
  • 5
  • What's the point of that one? You suggest starting a new intent from onDestroy()? And what should it do? – azizbekian May 21 '13 at 11:41
  • because before you get the return value from HttpPOSTer.logout your activity will be destroyed. IntentService will do the task and end itself so put this code inside intentservice and have a toast display the logout staus – prvn May 21 '13 at 11:52
  • Ahh, indeed, I forgot about it! Thank you, I'll try it now. – azizbekian May 21 '13 at 11:58