Question

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()?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top