user3626957
user3626957

Reputation: 11

Count up timer on multiple activities

I have a count up timer in one of my activities and I want to show that timer with elapsed time in my 2nd activity. I tried to Use Fragments for this but the timer keeps restaring on the launch of 2nd activity.

My timer code is:

startTime = SystemClock.uptimeMillis();
    customHandler.postDelayed(updateTimerThread, 0);

    private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
    /*  Timer.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));*/
        customHandler.postDelayed(this, 0);
    }

};

Upvotes: 0

Views: 153

Answers (1)

kabuko
kabuko

Reputation: 36302

Pass the startTime from the first activity to the second in the Intent. In the second activity, don't set startTime to SystemClock.uptimeMillis() but instead read it from the extras in the Intent sent from the first activity.

Upvotes: 1

Related Questions