AsycTask thread inside doInBackground runs after PostExecution has started - android

I am using AsyncTask and there are threads running inside the doInBackground method, isn't the purpose of AsyncTask is to let all the code finish executing inside the doInBackground and THEN go to PostExecute? Then why is it that some of my threads are running after the code block in PostExecution has started?
What should I do to solve this problem?
public class myActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
#Override
public void onClick(View view) {
new myTask().execute();
}
class myTask extends AsyncTask<Void, Void, Void> {
Boolean success;
#Override
protected void onPreExecute() {
success = true
}
#Override
protected Void doInBackground(Void... params) {
try {
Callback callback = new Callback() {
public void successCallback(String name, Object response) {
}
public void errorCallback(String name, error) {
success = false;}
};
} catch (Exception e) {
success = false;
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
if (success == true){
// do something
}
}
}
}
It is inside the error callback, that I want the success field to change to false. But the error callback runs after the postExecute method.

I think that you misunderstand the threading model.
AsyncTask does indeed execute doInBackground first, on a background thread, and then executes onPostExecute on the foreground thread, passing it the result from doInBackground.
However, from the wording of your question, it sounds like you are starting new threads from doInBackground. I'm sure that doInBackground does indeed complete before onPostExecute starts, but there is nothing that would cause the AsyncTask to wait for your additional threads to complete as well.
Edit:
It looks like you could skip the AayncTask altogether. The reason that you have callbacks is probably that method is already asynchronous
But note that you are only creating the callback, never using it. Perhaps you just left that part out?

Related

Abort Jsoup request [duplicate]

I use an async task to upload an image and get some results.
While uploading the image I see a progress dialog, written in onPreExecute() method like this:
protected void onPreExecute() {
uploadingDialog = new ProgressDialog(MyActivity.this);
uploadingDialog.setMessage("uploading");
uploadingDialog.setCancelable(true);
uploadingDialog.show();
}
Ok when I press the back button, obviously the dialog disappears because of the setCancelable(true).
But (obviously) the async task doesn't stop.
So how can I fix this? I want to cancel both dialog and async task when I press the back button. Any ideas?
From SDK:
Cancelling a task
A task can be cancelled at any time by invoking cancel(boolean).
Invoking this method will cause subsequent calls to isCancelled()
to return true.
After invoking this method, onCancelled(Object), instead of
onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.
To ensure that a task is cancelled as quickly as possible,
you should always check the return value of isCancelled() periodically from
doInBackground(Object[]), if possible (inside a loop for instance.)
So your code is right for dialog listener:
uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
myTask.cancel(true);
//finish();
}
});
Now, as I have mentioned earlier from SDK, you have to check whether the task is cancelled or not, for that you have to check isCancelled() inside the onPreExecute() method.
For example:
if (isCancelled())
break;
else
{
// do your work here
}
FOUND THE SOLUTION:
I added an action listener before uploadingDialog.show() like this:
uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
myTask.cancel(true);
//finish();
}
});
That way when I press the back button, the above OnCancelListener cancels both dialog and task. Also you can add finish() if you want to finish the whole activity on back pressed. Remember to declare your async task as a variable like this:
MyAsyncTask myTask=null;
and execute your async task like this:
myTask = new MyAsyncTask();
myTask.execute();
I spent a while figuring this out, all I wanted was a simple example of how to do it, so I thought I'd post how I did it. This is some code that updates a library and has a progress dialog showing how many books have been updated and cancels when a user dismisses the dialog:
private class UpdateLibrary extends AsyncTask<Void, Integer, Boolean>{
private ProgressDialog dialog = new ProgressDialog(Library.this);
private int total = Library.instance.appState.getAvailableText().length;
private int count = 0;
//Used as handler to cancel task if back button is pressed
private AsyncTask<Void, Integer, Boolean> updateTask = null;
#Override
protected void onPreExecute(){
updateTask = this;
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
updateTask.cancel(true);
}
});
dialog.setMessage("Updating Library...");
dialog.setMax(total);
dialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
for (int i = 0; i < appState.getAvailableText().length;i++){
if(isCancelled()){
break;
}
//Do your updating stuff here
}
}
#Override
protected void onProgressUpdate(Integer... progress){
count += progress[0];
dialog.setProgress(count);
}
#Override
protected void onPostExecute(Boolean finished){
dialog.dismiss();
if (finished)
DialogHelper.showMessage(Str.TEXT_UPDATELIBRARY, Str.TEXT_UPDATECOMPLETED, Library.instance);
else
DialogHelper.showMessage(Str.TEXT_UPDATELIBRARY,Str.TEXT_NOUPDATE , Library.instance);
}
}
create some member variables in your activity like
YourAsyncTask mTask;
Dialog mDialog;
use these for your dialog and task;
in onPause() simply call
if(mTask!=null) mTask.cancel();
if(mDialog!=null) mDialog.dismiss();
I would like to improve the code. When you canel the aSyncTask the onCancelled() (callback method of aSyncTask) gets automatically called, and there you can hide your progressBarDialog.
You can include this code as well:
public class information extends AsyncTask<String, String, String>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0) {
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
this.cancel(true);
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
#Override
protected void onCancelled() {
Toast.makeText(getApplicationContext(), "asynctack cancelled.....", Toast.LENGTH_SHORT).show();
dialog.hide(); /*hide the progressbar dialog here...*/
super.onCancelled();
}
}
Most of the time that I use AsyncTask my business logic is on a separated business class instead of being on the UI. In that case, I couldn't have a loop at doInBackground(). An example would be a synchronization process that consumes services and persist data one after another.
I end up handing on my task to the business object so it can handle cancelation. My setup is like this:
public abstract class MyActivity extends Activity {
private Task mTask;
private Business mBusiness;
public void startTask() {
if (mTask != null) {
mTask.cancel(true);
}
mTask = new mTask();
mTask.execute();
}
}
protected class Task extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onCancelled() {
super.onCancelled();
mTask.cancel(true);
// ask if user wants to try again
}
#Override
protected Boolean doInBackground(Void... params) {
return mBusiness.synchronize(this);
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
mTask = null;
if (result) {
// done!
}
else {
// ask if user wants to try again
}
}
}
public class Business {
public boolean synchronize(AsyncTask<?, ?, ?> task) {
boolean response = false;
response = loadStuff(task);
if (response)
response = loadMoreStuff(task);
return response;
}
private boolean loadStuff(AsyncTask<?, ?, ?> task) {
if (task != null && task.isCancelled()) return false;
// load stuff
return true;
}
}
I had a similar problem - essentially I was getting a NPE in an async task after the user had destroyed the activity. After researching the problem on Stack Overflow, I adopted the following solution:
volatile boolean running;
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
running=true;
...
}
public void onDestroy() {
super.onDestroy();
running=false;
...
}
Then, I check "if running" periodically in my async code. I have stress tested this and I am now unable to "break" my activity. This works perfectly and has the advantage of being simpler than some of the solutions I have seen on SO.
You can just ask for cancellation but not really terminate it. See this answer.
How to cancel AsyncTask
Full answer is here - Android AsyncTask Example
AsyncTask provides a better cancellation strategy, to terminate currently running task.
cancel(boolean mayInterruptIfitRunning)
myTask.cancel(false)- It makes isCancelled returns true. Helps to cancel the task.
myTask.cancel(true) – It also makes isCancelled() returns true, interrupt the background thread and relieves resources .
It is considered as an arrogant way, If there is any thread.sleep() method performing in background thread, cancel(true) will interrupt background thread at that time. But cancel(false) will wait for it and cancel task when that method completes.
If you invoke cancel() and doInBackground() hasn’t begun execute yet. onCancelled() will invoke.
After invoking cancel(…) you should check value returned by isCancelled() on doInbackground() periodically. just like shown below.
protected Object doInBackground(Params… params) {
while (condition)
{
...
if (isCancelled())
break;
}
return null;
}

Async error "Current thread must have a looper" when clicking retry button

I have an app that reads some data from a website and then creates TextViews for what is retrieved from the website. I have the process working through an AsyncTask. I've got it set up so that if there is a network error while trying to read from the website, a Retry button is shown. My code works perfect when it runs through the first time, but when I try to run the code from the onClick of the button, I get the following error:
java.lang.RuntimeException: An error occured while executing doInBackground()
(a few lines of error code)
Caused by: java.lang.IllegalStateException: The current thread must have a looper!
I even tried to have the onClick call an outside method as I saw someone recommend, but that didn't help. Here is some of the relevant code:
Async Task
private class DownloadListingTask extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... urls){
showLoadingPage();
try{
return getList(urls[0]);
}
catch (IOException e){
return sharedPreferences.getString("list_cache", "");
}
}
#Override
protected void onPostExecute(String result){
formatList(result);
}
}
Calling method
private void tryDownload(){
DownloadListingTask downloadListingTask = new DownloadListingTask();
downloadListingTask.execute(url);
}
onClick event
retryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tryDownload();
}
});
So when the tryDownload() method is called from onCreateView it works fine, but when I try it from the onClick is when I get that error.
This code worked for me:
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
primaryProgressBar.setVisibility(View.GONE);
}
});
For those who are still looking for answers and are using RxJava for async operations, this might be helpful.
When using Rxjava if you don't specify the observing thread then this error will come.
Caused by: java.lang.IllegalStateException: The current thread must have a looper!
so don't forget to add
subscribeOn(Schedulers.io())
observeOn(AndroidSchedulers.mainThread()) //required
disposableObserver = Observable.timer(2000, TimeUnit.MICROSECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
#Override
public void accept(Long aLong) throws Exception {
//some code
}
});
Try to call showLoadingPage in onPreExecute method:
private class DownloadListingTask extends AsyncTask<String, Void, String>{
#Override
protected void onPreExecute(){
showLoadingPage();
}
#Override
protected String doInBackground(String... urls){
try{
return getList(urls[0]);
}
catch (IOException e){
return sharedPreferences.getString("list_cache", "");
}
}
#Override
protected void onPostExecute(String result){
formatList(result);
}
}

submitScoreGPGS does not work with AsyncTask

I am trying to get the Google Play Services working as following using AsyncTaks:
1 - The Login Dialog is only displayed when the user click on the leaderboard button which call the method loginGPRS() below;
2 - An ansync task is then executed which call the startLoginGRPS() method on the onPreExecute
3 - [Here is the problem] Once he is logged in, I want to call the methods submitScoreGPRS() and getLeaderBoardGPRS() in onPostExecute method, but the leaderboard dialog is never opened...
Here is the relevant source code:
#Override
public void loginGPGS() {
try {
MyAsyncTask asyncTask_a = new MyAsyncTask();
asyncTask_a.execute();
} catch (final Exception ex) {
}
}
class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
String errorMsg;
#Override
protected void onPreExecute() {
startLoginGPGS();
super.onPreExecute();
}
#Override
protected Boolean doInBackground(Void... v) {
return true;
}
#Override
protected void onPostExecute(Boolean success) {
if(success){
submitScoreGPGS(bestScore);
getLeaderboardGPGS();
}
}
}
public void startLoginGPGS() {
try {
// runOnUiThread(new Runnable() {
// public void run() {
gameHelper.beginUserInitiatedSignIn();
// }
// });
} catch (final Exception ex) {
}
}
#Override
public void submitScoreGPGS(int score) {
if(getSignedInGPGS()){
Games.Leaderboards.submitScore(gameHelper.getApiClient(),
getString(R.string.leaderboard1_id), score);
}
}
Have you trace your code??
Write something like...
Log.d("Trace","Point X"):
In the doInBackground method just before the return true, another one in the onPostExecute method just before the if and inside the if.
Take a look at your Logcat and come back...
Hope it helps.
You're executing your method on the UI thread, because onPostExecute() runs on the UI thread and your method has network execution..
You should move your method to the doInBackground() method

AsyncTask run 5 times and then not run doInBackground method

click the button to call this method:
private void goRegister(final boolean isUsername) {
new Loading.LoadTast(ctx) {
#Override
protected String doInBackground(Integer... params) {
Looper.prepare();
String msg=doRegister(isUsername);
closeProgressDialog();
if(msg == null || msg.length() == 0) {
SmartNgApplication.getInstance().exit();
} else {
BaseHelper.showToast(ctx, msg);
}
Looper.loop();
return null;
}
}.execute();
}
and this is LoadTast class:
public abstract static class LoadTast extends AsyncTask <Integer, Integer, String > {
private ProgressDialog progressDialog;
private Context ctx;
public LoadTast(Context ctx) {
this.ctx=ctx;
}
protected abstract String doInBackground(Integer... params);
public void onPreExecute() {
super.onPreExecute();
progressDialog=ProgressDialog.show(ctx, "", "loading...", true, false);
}
public void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
BaseHelper.showToast(ctx, result);
}
public void closeProgressDialog() {
if(progressDialog != null) {
progressDialog.dismiss();
}
}
}
my program is:
when I click 5 times the button don't call doInbackground method and the screen always run a loading. I guess the code is run onPreExecute and not run doInbackground. Why??
in the AsyncTask has a thread pool,the CORE_POOL_SIZE=5. How to solution the program,Help me thank you !
asyncTask isn't supposed to run its doInBackground more than once , and it's also not supposed to be a loopy thread .
asyncTask is supposed to be a one time thing that you run , and you can only assume that if multiple asyncTasks have executed , at least one will run at the same time.
another thing i've noticed : you called closeProgressDialog from within doInBackground , but that's a UI related operation ,while the doInBackground is being ran on a non-UI thread.
I have had this problem myself. I solved it by executing the AsyncTasks in the thread pool, instead of serial.
To do this, you have to call your AsyncTasks like this:
YourAsyncTask task = new YourAsyncTask();
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
That should solve the issue you have.

ProgressBar display with some delay in on click of option menu

I am facing the issue with displaying progressbar onItem selected in option menu.
My code is here:
case R.id.mnuLogout:
showDialog(Constants.PROGRESS_DIALOG);
closeOptionsMenu();
if(MyApp.IsLoggedOut())
handler.sendEmptyMessage(Constants.LOGOUT);
else
handler.sendEmptyMessage(Constants.ERROR_MSG);
Progressbar is displayed after completion of IsLogged method.
You're calling get() right after the AsyncTask as executed and lose asynchronous behavior because this method waits until task is finished. You should add all the code in try/catch block to AsyncTask.onPostExecute() method and also dismiss the dialog from this method.
void doLogout() {
new LogoutTask().execute();
}
void dispatchLogoutFinished() {
dismissDialog(Constants.PROGRESS_DIALOG);
if (MyApp.IsLoggedOut()) {
// do something
} else {
// do something else
}
}
private class LogoutTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
TheActivity.this.showDialog(Constants.PROGRESS_DIALOG);
}
protected Void doInBackground(Void... params) {
return null;
}
protected void onPostExecute(Long result) {
TheActivity.this.dispatchLogoutFinished();
}
}
And I don't think you need to send messages to the handler. The dispatchLogoutFinished() is executed on the UI thread, so there's no need for synchronization.

Categories

Resources