Thursday, August 22, 2013

Canceling an AsyncTask using the back button

After the last post about AsyncTask, there is a pending task to complete the example. A task running in background must be canceled if the user demand it. Thus, this is a good practice promoted by Google. Applications must be responsives. As well as canceling a task, we are going to learn how to capture the back button. AsyncTask will be canceled when back button is pressed by the user.

For capturing back button since Android 2.0 (Api level 5), Google has implemented a new method inActivity class which must be overridden. This method is onBackPressed(). In earlier versions, back button is captured with method onKeyDown() and is identified checking if key received as parameter is KeyEvent.KEYCODE_BACK.

So, in ProgressBarExampleActivity class from the last post we need two changes:
  • Add onBackPressed() method.
  • Set ProgressBarAsyncTask class as member of the class to be cancelled fromonBackPressed() method.
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class ProgressBarExampleActivity extends Activity {
   private static final String LOG_TAG = "PB_EXAMPLE";
   private EditText etNumSecondsM;
   private EditText etSecondsProgressedM;
   private Button bExitM;
   private Button bExecuteM;
   private ProgressBar pbDefaultM;
   private ProgressBarAsyncTask pbTaskM = null;
   /** Called when the activity is first created. */
   public void onCreate(Bundle savedInstanceState) {
   /** Called when user press the back button */
   public void onBackPressed()
      Log.d(LOG_TAG, "Cancelling task");
      if( pbTaskM != null)
         pbTaskM.cancel( false);
   public void drawGUI()
      Log.d(LOG_TAG, "Creating Graphic Interface");
      //Text Editors
      etNumSecondsM = (EditText) findViewById(;
      etSecondsProgressedM = (EditText) findViewById(;
      bExecuteM = (Button) findViewById(;
      bExitM = (Button) findViewById(;
      //Progress Bar
      pbDefaultM = (ProgressBar) findViewById(;   
      // When the execution button is pressed, the AsyncTask is created and
      // executed.
      bExecuteM.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               pbTaskM = new ProgressBarAsyncTask( pbDefaultM, etSecondsProgressedM);
               pbTaskM.execute( new Integer( etNumSecondsM.getText().toString()));
      bExitM.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
             exit( RESULT_OK);
    public void exit( int theResult)
       setResult( theResult);

No comments:

Post a Comment