trying to cancel AsyncTask almost there just a little push

im trying to get the cancel a loop by calling cancel but it wont cancel out out. can you tell why it wont cancel out.. here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

class MyAsyncTask extends AsyncTask<Object, Void, Void>
{


@Override
protected void onCancelled() {
super.onCancelled();

this.cancel(true);

Log.d( String.valueOf( isCancelled() ), "onCancelled() called");


}



@Override
protected Void doInBackground(Object... params)
{
while (!isCancelled())
{
Log.d( String.valueOf( isCancelled() ), "inside the loop");
if (isCancelled())
break;
}
return null;
}
}

1
2
3
here is the call

new MyAsyncTask().execute();

1
2
3
4
5
6
and here is the call to cancel it
it returns true in the "protected void onCancelled()" but not in "protected Void doInBackground" 

AsyncTask task = new MyAsyncTask().execute();
task.cancel(true);



Last edited on
Here is how to make your code readable on the forum; put
[code]
before code, and
[/code]
after code.

As it is, this looks like Java code. You might have better luck on a Java forum.
Last edited on
Topic archived. No new replies allowed.