Transmitting a countdown timer
I am trying to pass the CountDownTimer value from Activity
1 ( PrimerNivel)
to Activity
2 ( SegundoNivel
) and run CountDownTimer
to Activity
2 from the value obtained from Activity
1.
But CountDownTimer
at Activity
2 is reset to zero. I cannot find the error. can anyone help me?
This is the code Action 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();
}};
}
Step 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);
}}
source to share
You are creating CountDownTimer
before you get the value. Just move the code that creates the timer below where you get the value.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.juego);
cuentaRegresiva=(TextView)findViewById(R.id.cuentaRegresiva);
bundle = getIntent().getExtras();
startTime= bundle.getLong("regresivaAnterior")/1000;
// move it here after you've gotten the value
countDownTimer = new MyCountDownTimer(startTime, interval);
cuentaRegresiva.setText(""+startTime);
countDownTimer.start();
You may need to cancel your first timer to get the correct value at the right time. Try to call
countDownTimer.cancel(); countDownTimer = null;
before creation Intent
. Also, you divide startTime
by 1000 after you get the value from Intent
. I'm not sure if you want to do this.
source to share