问题
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