How to cancel AsyncTask
I found great solution to solve the AsyncTask cancel problem
found this so annoying to solve but it fixed the cancelation problem of AsyncTask of mine so sharing with you guys
when i was using Android emulator and running Oreo version on it and trying
asyncTaskObj.cancle(true);
it was perfectly working i.e it was perfectly calling onCancelled() method of AsyncTask
but problem arise when i tried to run it in my Marshmallow version android device, it was not calling onCancelled() method of AsyncTask so i found a solution where we call asyncTaskObj.cancle(true); i.e. in onClick event we can do version check and if version is smaller then Oreo then manually call onCancelled() method and also check if isCancelled() is true i.e. Eg:
mButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//set state of cancel for AsyncTask
myAsyncTask.cancel(true);
//checking if version is samller then Oreo
if (myAsyncTask.isCancelled() && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
myAsyncTask.onCancelled();
}
});
and then in onCancelled() method of AsyncTask we can do
if (isCancelled()){
}
Eg:
@Overrideprotected void onCancelled() {
super.onCancelled();
if (isCancelled()) {
//write all your after cancellation code here
InputMsg.showToastShort(mContext, "Downloading canceled! Status: " + isCancelled());
InputMsg.showLog("isCancelled(): " + isCancelled());
}
}
in this was onCancelled() method also will not be called two times in Oreo version of android and onCancelled() will be called once in all version of android for per object of AsyncTask
Eg code:
public class MainActivity extends AppCompatActivity {
ProgressBar mProgressBar;
Button mButtonStart, mButtonCancel;
MyAsyncTask myAsyncTask;
Context mContext = MainActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mButtonStart = (Button) findViewById(R.id.buttonStart);
mButtonCancel = (Button) findViewById(R.id.buttonCancel);
mProgressBar.setMax(100);
mProgressBar.setVisibility(View.INVISIBLE);
mButtonCancel.setEnabled(false);
mButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myAsyncTask = new MyAsyncTask();
mButtonCancel.setEnabled(true);
myAsyncTask.execute();
}
});
mButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//set state of cancel for AsyncTask
myAsyncTask.cancel(true);
//checking if version is samller then Oreo
if (myAsyncTask.isCancelled() && Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
myAsyncTask.onCancelled();
}
});
}
private class MyAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
protected Boolean doInBackground(Void... voids) {
for (int i = 1; i <= 100; i++) {
publishProgress(i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
InputMsg.showLog(e.getMessage());
}
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressBar.setProgress(values[0]);
if (values[0] == 1) {
mButtonStart.setEnabled(false);
mButtonCancel.setEnabled(true);
} else if (values[0] == 100) {
mButtonStart.setEnabled(true);
mButtonCancel.setEnabled(false);
}
}
@Override
protected void onPostExecute(Boolean aBoolean) {
InputMsg.showToastLong(mContext, "Status: " + aBoolean);
mProgressBar.setVisibility(View.INVISIBLE);
}
@Override
protected void onCancelled() {
super.onCancelled();
if (isCancelled()) {
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.INVISIBLE);
mButtonStart.setEnabled(true);
mButtonCancel.setEnabled(false);
InputMsg.showToastShort(mContext, "Downloading canceled! Status: " + isCancelled());
InputMsg.showLog("isCancelled(): " + isCancelled());
}
}
}
}
This worked for me so shared this post
hope this helps :)
Comments
Post a Comment