tags:

views:

239

answers:

2

Hi,

I'm extending the CountDownTimer class to obtain some custom functionality .In onTick() in case some conditions are met I call cancel() , expecting that will be the end of it, however the onTick() callback gets call until the the count down is reached . So how to prevent this from happening ?

+1  A: 

CountDownTimer.cancel() method seems to be not working. Here's another thread without a solution http://stackoverflow.com/questions/2377485/timer-does-not-stop-in-android.

I would recommend you to use Timer instead. It's much more flexible and can be cancelled at any time. It may be something like that:

public class MainActivity extends Activity {    
    TextView mTextField;
    long elapsed;
    final static long INTERVAL=1000;
    final static long TIMEOUT=5000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextField=(TextView)findViewById(R.id.textview1);

        TimerTask task=new TimerTask(){
            @Override
            public void run() {
                elapsed+=INTERVAL;
                if(elapsed>=TIMEOUT){
                    this.cancel();
                    displayText("finished");
                    return;
                }
                //if(some other conditions)
                //   this.cancel();
                displayText("seconds elapsed: " + elapsed / 1000);
            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, INTERVAL, INTERVAL);
    }

    private void displayText(final String text){
        this.runOnUiThread(new Runnable(){
            @Override
            public void run() {
                mTextField.setText(text);
            }});
    }
}
Fedor
A: 

CountDownTimer.cancel() is working fine for me. I had some issues with it when the screen orientation was changed. Multiple dspCounterObj objects were created when screen orientation changed.

dspCounterObj = new DspCounter(MyApplication.getTimeCount()*1000,1000);
dspCounterObj.start();

I am total Java/Android newbie, i may be wrong, but this is how i solved this.

Every time i create dspCounterObj, i store that in the Application class and before storing i will get the reference of previously created objects and cancel it.

if(MyApplication.getTimeObjSize()>0){
           for(int i=0;i<MyApplication.getTimeObjSize();i++){
              tempDSPCounterObj= (DspCounter)MyApplication.getTimeObj(i);
              tempDSPCounterObj.cancel();
           }
       }

dspCounterObj = new DspCounter(MyApplication.getTimeCount()*1000,1000);
MyApplication.setTimeObj(dspCounterObj);
dspCounterObj.start()

public class MyApp extends Application {
..
..
private ArrayList<Object> timeObj;


    public int getTimeObjSize(){
        return this.timeObj.size();
    }
    public Object getTimeObj(int i){
        return timeObj.get(i);
    }

    public void setTimeObj(Object timeObj){
        this.timeObj.add((Object)timeObj);
    }


    public void onCreate(){
        super.onCreate();
        this.timeObj = new ArrayList<Object>(0);
    }


}

For some reason I think this is overkill. please don't throw rocks at me.

Amaacnt27