Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8196711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:34:30+00:00 2026-06-07T05:34:30+00:00

I have a CountdownTimer that counts down from 60 seconds. This CountdownTimer works by

  • 0

I have a CountdownTimer that counts down from 60 seconds. This CountdownTimer works by setting a textView to the remaining milliseconds, but whenever i rotate my device, the CountdownTimer gets reset.

I know this happens because the Activity gets restarted on rotation. So i tried saving the time remaining in a bundle and then restoring it, after the Activity was restarted.

long transferValue;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playtimemode);

    Log.d("Debug", "onCreate: " + transferValue);

    long setTime = 60000;
    long difference = setTime - transferValue;

    new CountDownTimer(difference, 1000) {

        public void onTick(long millisUntilFinished) {
            millisUntilFinishedToSave = millisUntilFinished;
            tvCountdown.setText("" + millisUntilFinished / 1000);
        }

        public void onFinish() {
            tvCountdown.setText("done!");
        }
    }.start();

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putLong("millisKey", millisUntilFinishedToSave);

}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    transferValue = savedInstanceState.getLong("millisKey");
    Log.d("Debug", "onRestoreInstanceState(): " + transferValue);
}

This however doesn’t work. I am intializing transferValue at the top of this code (hence it returning 0), but how can i else save the data from the savedInstanceState to the CountdownTimer?

07-06 20:21:30.038: D/Debug(28995): onCreate: 0
07-06 20:21:30.043: D/Debug(28995): onRestoreInstanceState(): 55994
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:34:31+00:00Added an answer on June 7, 2026 at 5:34 am

    I would give your timer it’s own thread. Your timer is being stopped because it’s on the UI thread (as you stated) and when the UI redraws the Activity is re-initialized. All long running processes should have their own thread. Rule of thumb: get out of the UI thread as soon as possible.

    Here is an example of using a Service. This service will start when called and stay in memory regardless of screen orientation changes or even activity focus changes.

    Here is the service:

    public class Timer extends Service {
    
        @Override
        public IBinder onBind(Intent i) {
            return null;
        }
        @Override
        public int onStartCommand(Intent i, int flags, int startId) {
            // Put your timer code here
        }
    }
    

    You will need to add this line to your manifest (somewhere between the application open/close tags):

    <service android:name=".Timer" />
    

    Then you can start the service at any time by using this (it’s important to note that you can start it over and over again, the recursive calls do not make a new service.):

    startService(new Intent(this, Timer.class));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that uses a CountDownTimer that counts down from 10. How
Hello I have an Activity that shows a CountDown in a textView (using CountDownTimer),
I have a widget that, when pressed, sends a broadcast that starts a CountDownTimer
I have this code: package com.powergroupbd.timer; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer;
I have a a counter that needs to count up from $0 to $10,000
I have a countdown timer that displays the number of seconds left on a
I have a javascript countdown timer that works by taking a specified date and
I have an Android app that has separate things going on but are all
I have developed an application which starts a custom service that fires a countdowntimer.
I have got a page on a website, that works similar to a reverse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.