2
package com.example.asynctask;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
    ProgressBar progressbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progressbar = (ProgressBar) findViewById(R.id.progressBar);
    }

    class ProgressTask extends AsyncTask<Integer, Integer, Void> {

        private boolean flag = true;

        @Override
        protected void onPreExecute() {
            progressbar.setMax(100);
        }

        @Override
        protected void onCancelled() {
            //progressbar.setMax(0);
            flag = false;
            Log.v("onCancelled:flag", String.valueOf(flag));
        }

        @Override
        protected Void doInBackground(Integer... params) {
            // TODO Auto-generated method stub
            int start = params[0];
            for (int i = start; i <= 100; i=i+10) {

                if (!flag) {
                    break;
                } else {
                    publishProgress(i);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("ThreadError", e.toString());
                    }
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            progressbar.setProgress(values[0]); 
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.v("Progress", "Finish");
        }

    }

    public void onClick(View v) {
        ProgressTask task = new ProgressTask();

        switch (v.getId()) {
        case R.id.start:
            task.execute(10);
            break;

        case R.id.stop:
            task.cancel(true);
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Here is a symple AsyncTask app which starts to update the progress bar when you hit Start. But when you will hit stop, it wont stop. Thing is when you call task.cancel(true), onCancelled() methods does gets invoked and changes the value of flag to false but in onCancelled() value of flag remains true. I have also tried isCancelled() in place of flag varaible with no success. For a moment my problem is similar to Ollie C, but no exceptions are thrown in my case.

Community
  • 1
  • 1
nik
  • 8,387
  • 13
  • 36
  • 44
  • 3
    In this line `ProgressTask task = new ProgressTask();`. Your `task` is in the scope of that method. You should create an class instance `task`. – Wenhui Oct 09 '12 at 03:25

1 Answers1

0

As Wenhui pointed out in the comment, I made a silly mistake in which I declared and instantiated task inside onClick(View v).

nik
  • 8,387
  • 13
  • 36
  • 44