AsyncTask in onDestroy() not being executed

﹥>﹥吖頭↗ 提交于 2019-12-10 12:10:25

问题


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:

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


回答1:


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



来源:https://stackoverflow.com/questions/16668804/asynctask-in-ondestroy-not-being-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!