Reputation: 551
i have a try and catch in "doInBackground", i dont wan the code be run the onPostExecute if the apps execute catch method in "doInBackground". code as below:
public class DownloadFileAsync extends AsyncTask<String, Integer, Void> {
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Void doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
}
catch{
//some code, don't do onPostExecute after complete
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
//some code
}
@Override
protected void onPostExecute(Void unused) {
//some code
}
}
Upvotes: 1
Views: 2577
Reputation: 4784
Use a Pair so you can return two results
Example, replace ANYTHING with the type that you want to return:
public class DownloadFileAsync extends AsyncTask<String, Integer, Pair<Exception, ANYTHING> > {
@Override
protected Pair<Exception, ANYTHING> doInBackground(String... aurl) {
try{
return new Pair<Exception, ANYTHING>(null, /* anything can go here */);
}
catch (Exception e) {
return new Pair<Exception, ANYTHING>(e, null);
}
}
@Override
protected void onPostExecute(Pair<Exception, ANYTHING> result) {
// Evaluate the exception here
if(result.first != null) {
// an eror occured, do something about it
}
}
}
This way allows you to handle an exception in onPostExecute as well as get the result from result.second if everything went ok.
Upvotes: 1
Reputation: 18592
You may try using cancel(true)
in doInBackground()
. Try the following code:
protected Void doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
}
catch{
cancel(true);
}
return null;
}
The official note on cancel()
Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. Calling this method guarantees that onPostExecute(Object) is never invoked. After invoking this method, you should check the value returned by isCancelled() periodically from doInBackground(Object[]) to finish the task as early as possible.
Check this link for further details:
http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
Upvotes: 6
Reputation: 4341
You can have a boolean variable in your AsyncTask
and check for that variable. For example:
public class DownloadFileAsync extends AsyncTask<String, Integer, Void> {
private boolean run_do_in_background = true;
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Void doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
}
catch{
//some code, don't do onPostExecute after complete
run_do_in_background = false;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress) {
//some code
}
@Override
protected void onPostExecute(Void unused) {
if(run_do_in_background) { //only if this is true will excecute the code in doInBackground
//some code
}
}
}
Upvotes: 1
Reputation: 109247
Return boolean data type from your doInBackground()
to onPostExecute()
of your AsyncTask. And check for its values what you want to do for success,
@Override
protected boolean doInBackground(String... aurl) {
try{
//some code, jump to onPostexecute after complete
return true;
}
catch{
//some code, don't do onPostExecute after complete
return false;
}
}
and
@Override
protected void onPostExecute(boolean flag) {
if(flag)
{
//some code
}
}
Upvotes: 3