tags:

views:

23

answers:

1

My activity has a simple menuItem that start a counter... the counter is stored in a field of the activity the problem is with the "back button" after closing the activity (back button), the timer (as in my intention) continues, and restarting the activity behave correctly by reshowing the timer... but I lost the reference to the counter and so I cannot stop it Any ideas?

following there is a part of the code of the function boolean onOptionsItemSelected(MenuItem item)

switch (item.getItemId()) {
    case R.id.STARTSTOP:
        running = !running;
        if (!running) {
            item.setTitle("START");
            if (counter!=null) {
                counter.cancel();
                counter = null;
            }
        }
        else {
            item.setTitle("STOP");
            counter = new CountDownTimer(1000*60*20/*20min*/,1000){
                @Override
                public void onFinish() {
                    TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText("done!");
                }
                @Override
                public void onTick(long millisUntilFinished) {
                    TextView tv = (TextView) findViewById(R.id.counter);
                    tv.setText(millisUntilFinished);
                    Log.d("XXXDEBUGXXX",millisUntilFinished);
                }
            };
            counter.start();
    }

}

A: 

I'd suggest to use AlarmManager instead.
Or you could use SharedPreferences to store intermediate value that's read in onResume(..) to restart the timer and saved in onPause(..).

alex
in your solution (if I understood) I should stop the counter, save in the preferences the reached time, and on resume, take that time and start another counter.but this is not the behaviour that I want. I want that the counter continues the countdown (and it did it) and then be able to stop it (i.e., find reference to it)I'll look to AlarmManager if that helps... thanks
@user290302 you save the reached time + the current timestamp and then subtract the time elapsed since the last stop from the reached time before starting a new timer.
alex
yes, but in this manner if the timer timeouts in the meanwhile (during the phase where the activity is not shown) I would have no notification you are right: in the code it is not presented, but it is intended that at the timeout it puts a notification in the notification bar... :-D
In this case AlarmManager is all you need.
alex
could you give me a good link to understand how can show a countdown with alarmmanager? :-D
Google it. :)In my app AlarmManager starts a Service that then shows Notification. `alarmManager.set(RTC_WAKEUP, timestamp, scheduleIntent);`
alex
I did it...I think that I'll use a mix of the two solution...-CountDownTimer to show the count down (using the stop and restart with offset, but probably with Bundle and SaveInstanceState)-AlarmManager to set the final notification if not cancelledhowever I am little surprised that there is no way to recover the reference to the started (and not stopped and working) CountDownTimer