Gio31290
Gio31290

Reputation: 87

How to show Timer's remaining time in all activities?

I want to create a CountDownTimer passing its remaining time through all activities and show it in TextViews. I tried using intent.putExtra but when i start the new Activity the Timer restarts.

This is what I need:

-ActivityA starts with a 60 seconds timer, so its TextView shows 59...58...57...etc...

-ActivityB starts after 6 seconds so its TextView shows 54...53...52...etc...

The Countdown simply have to go on from first to last activity.

Hope I was clear enough. Thank you for solutions and sorry for my Ital-English.

Upvotes: 2

Views: 1671

Answers (1)

Manuel Allenspach
Manuel Allenspach

Reputation: 12735

use a static variable (maybe create a new class for "better" code).

public static int time;

Or you can declare it in your ActivityA, count the variable down and use it in ActivityB

textView.setText("" + ActivityA.time);

EDIT:

public class MyTimer {
    private static int time = 60;

    public MyTimer() {
        if(time >= 60) {
            countDown();  //let countDown only run once
        }
    }

    private void countDown() {
         //insert a thread here, which counts your time down
    }

    public int getTime() {
        return this.time;
    }

}

And then, in your acitivities:

MyTimer mTimer = new MyTimer();
textview.setText(mTimer.getTime() + "");

Upvotes: 1

Related Questions