I have a inner class that extends CountDownTimer. Basically its a simple countdown timer that updates a TextView in the activity and plays a sound when the timer is finished. The code for the inner class is:
public class SetTimer extends CountDownTimer
{
public SetTimer(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish()
{
timeLeft.setText("0");
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r=RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
}
@Override
public void onTick(long millisUntilFinished)
{
String t;
t=String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)
-TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
timeLeft.setText(t);
}
}
The code that creates and references the TextView is:
TextView timeLeft;
and in the onCreate method:
timeLeft=(TextView) findViewById(R.id.txtTimeLeft);
This works fine until I rotate the display. At that point the timer is still running and does play the sound at the end but it doesn't update the TextView. The TextView is declared at the top of the class and referenced in the onCreate method of the activity. If I restart the timer then it works. I used Log.d to check if the onTick method was still getting called and it was. My guess is that the reference to the TextView has changed but I can't figure out how to set it back to the timer. I tried declaring a TextView in the onTick method and updating that figuring it would then pick up a reference to the current instance of the TextView but that also didn't work. The only other thing to note is that the SetTimer object is created when the user clicks on a button. That code is:
timer=new SetTimer(interval, 100);
timer.start();
Any thoughts on how to have the SetTimer keep updating the TextView after the screen is rotated?