I'm trying to pass the a CountDownTimer value from Activity
1(PrimerNivel)
to Activity
2(SegundoNivel
)and start the CountDownTimer
in Activity
2 from the value that got from the Activity
1.
But the CountDownTimer
in Activity
2 is reset to zero. I can't find the error. could someone help me?
This is the code Activity 1:
public class PrimerNivel extends InicioActivity {
private TextView cuentaRegresiva;
long startTime = 60 * 1000;
private final long interval = 1 * 1000;
MyCountDownTimer countDownTimer;
long tiempoRestante;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.juego);
cuentaRegresiva=(TextView)findViewById(R.id.cuentaRegresiva);
countDownTimer = new MyCountDownTimer(startTime, interval);
cuentaRegresiva.setText(cuentaRegresiva.getText() + String.valueOf(startTime / 1000));
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
cuentaRegresiva.setText("");
}
@Override
public void onTick(long millisUntilFinished) {
tiempoRestante= millisUntilFinished;
cuentaRegresiva.setText(""+millisUntilFinished/1000);
}}
OnClickListener siguiente =new OnClickListener(){
public void onClick(View arg0) {
Intent aNivelSiguiente = new Intent(PrimerNivel.this, SegundoNivel.class);
aNivelSiguiente.putExtra("regresivaAnterior", tiempoRestante);
startActivity(aNivelSiguiente);
PrimerNivel.this.finish();
}};
}
Activity 2:
public class SegundoNivel extends InicioActivity {
private TextView cuentaRegresiva;
long startTime;
private final long interval = 1 * 1000;
MyCountDownTimer countDownTimer;
Bundle bundle;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.juego);
cuentaRegresiva=(TextView)findViewById(R.id.cuentaRegresiva);
countDownTimer = new MyCountDownTimer(startTime, interval);
bundle = getIntent().getExtras();
startTime= bundle.getLong("regresivaAnterior")/1000;
cuentaRegresiva.setText(""+startTime);
countDownTimer.start();
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
cuentaRegresiva.setText("");
}
@Override
public void onTick(long millisUntilFinished) {
cuentaRegresiva.setText(""+millisUntilFinished/1000);
}}
You are creating the
CountDownTimer
before getting the value. Simply move the code that creates the timer to below where you get the value.You may need to cancel your first timer to get the correct value at the correct time. Try calling
before creating the
Intent
. Also, you are dividing thestartTime
by 1000 after you get the value from theIntent
. I'm not sure you want to be doing that.