Note that there are some explanatory texts on larger screens.

plurals
  1. POAsyncTask in Android starts but does not process
    text
    copied!<p>I know that there are many similar <code>AsyncTask</code> questions already, but in my case something is very unusual or am I missing something !? </p> <p>As the <code>AsyncTask</code> is not allowed to run more than once. I call it with <code>new B().execute();</code> so it should create a separated instance on each run !? Right?</p> <p>The problem is after the B class is created and executed once, it wont work the second time the user calls the <code>startBClass()</code> method (just opens the dialog, but the actual work is not happening).</p> <p><strong>I just Debugged the code and realize that after the Dialog is closed, the Thread is still running in the background. What is the proper way to stop the background thread when the Dialog is closing?</strong> - And since I'm closing the first Dialog inside B class and create another instance of the B class, why is the second one not working? Can't multiple AsyncTasks run in parallel !? </p> <p>I simplified the classes for easier understanding what I'm trying:</p> <pre><code>public class A { /* Is called when user clicks a button */ private void startBClass() { new B().execute(); } /* Opens a Dialog with a countdown TextView (works first call only) */ private class B extends AsyncTask&lt;Void, Integer, Void&gt; { private int secondsPassed = 0; private double totalToPay = 0; private Dialog dialog; private TextView tvCost; private Button dialogBtn; @Override protected void onPreExecute() { super.onPreExecute(); dialog = new Dialog(ConfigurationActivity.this); dialog.setCancelable(true); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.dialog); dialog.setCanceledOnTouchOutside(false); dialog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { onPostExecute(null); } }); tvCost = (TextView) dialog.findViewById(R.id.textCounter); dialogBtn = (Button) dialog.findViewById(R.id.button1); dialogBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.cancel(); } }); dialog.show(); } @Override protected Void doInBackground(Void... arg0) { while(true){ publishProgress(secondsPassed++); SystemClock.sleep(1000); } } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); totalToPay = 12.00; tvCost.setText(totalToPay + " USD"); } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); final AlertDialog alert = new AlertDialog.Builder(ConfigurationActivity.this).create(); alert.setTitle("Information"); alert.setMessage("You should pay about " + totalToPay + " USD."); alert.setButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface alertDialog, int which) { alertDialog.dismiss(); } }); alert.show(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload