Note that there are some explanatory texts on larger screens.

plurals
  1. POI got stuck on the onBackPressed() in AsyncTask
    primarykey
    data
    text
    <p>I am start working on the Asynctask Demo. What I want is , "When I press the back button in the android device I want to stop the task which is running in the app "</p> <p>I set up this thing on the onBackPressed() method but after this in the Logcat Task doesn't stop .</p> <p>Where am I make the mistake , help me out of this ?</p> <pre><code>public class Activity1 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new CountDownTask().execute(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onBackPressed() { super.onBackPressed(); new CountDownTask().cancel(true); } private class CountDownTask extends AsyncTask&lt;Void, Integer, Void&gt;{ @Override protected void onPreExecute() { TextView tvCounter = (TextView) findViewById(R.id.tv_counter); tvCounter.setText("START"); } @Override protected Void doInBackground(Void... params) { for(int i=15;i&gt;=0;i--){ try { Thread.sleep(1000); publishProgress(i); Log.e("Aka", String.valueOf(i)); if(isCancelled()){ break; } } catch (InterruptedException e) { } } return null; } @Override protected void onProgressUpdate(Integer... values) { TextView tvCounter = (TextView) findViewById(R.id.tv_counter); tvCounter.setText( Integer.toString(values[0].intValue())); } @Override protected void onPostExecute(Void result) { TextView tvCounter = (TextView) findViewById(R.id.tv_counter); tvCounter.setText("DONE"); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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