Search code examples
javaandroidcountdowntimer

Passing countdown timer value to next activity


i'm trying to pass the countdown timer value as textview to next activity, but i don't know how, do i need do use intent.putextra() ? There is my code:

    countDownTimer = new CountDownTimer(5000, 1000) {
        @SuppressLint("DefaultLocale")
        public void onTick(long millisUntilFinished) {
            timpRamas.setText(String.format("%d:%d",
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
        }
        public void onFinish() {
            Intent intent = new Intent(c1_1.this,TimpExpirat.class);
            startActivity(intent);
            finish();
        }
    }.start();

Solution

  • You must pass the data of the textView to the next activity and in the next activity you can set the data in another view or do whatever with it:

        public void onFinish() {
            Intent intent = new Intent(c1_1.this,TimpExpirat.class);
            intent.putExtra("data" , timpRamas.getText().toString());
            startActivity(intent);
            finish();
        }
    

    In the TimpExpirat activity:

    //get the data in onCreate()
    
    Intent intent = getIntent();
    String time = intent.getStringExtra("data");
    
    //time now has the time that was last set on your textview, you can set it
    //to a new textview or do whatever with it.