AsyncTask execution need to cancel in same fragment when make request from Navigation Drawer's every List Items

时光怂恿深爱的人放手 提交于 2020-01-04 06:20:31

问题


My Question is i need to open SAME FRAGMENT from Navigation Drawer's every List Item 's, and when fragment display, If AsynTask execution is On Going then AsyncTask execution should be cancel.

How can i do it?

I tried so many ways to accomplish this but i failed everytime.

I used asynctaskObject.cancel(true);

So Can anybody please answer. i hope i'll get the result.

Thanks in advance. :)


回答1:


i found solution of my problem.

We just need to make request in onStop method like below :

@Override
public void onStop() {
    super.onStop();
    if(asyncTaskObject!=null){
        asyncTaskObject.cancel(true);
    }
}    

This way whenever we request from Navigation Drawer's List Item on Same Fragment, at that time fragment call onStop() method and i just call task.cancel(true);

And Thanks to @MysticMagic. i resolved this issue from your given link.




回答2:


For forceful cancel() to work, you will need to check if its cancelled in doInBackground.

@Override
protected String doInBackground(String... params) 
{
    . 
    .
    if(isCancelled()){
        break;
    }
    .
}

Refer this: Why Android AsyncTask doesn’t stop after being cancelled

Hope it helps.



来源:https://stackoverflow.com/questions/26923535/asynctask-execution-need-to-cancel-in-same-fragment-when-make-request-from-navig

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