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

  • Home
  • SEARCH
  • 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 6679697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:23:20+00:00 2026-05-26T04:23:20+00:00

I have a timer that counts up from the time a user encounters that

  • 0

I have a timer that counts up from the time a user encounters that activity

I am currently using a Chronometer set during onCreate (initially started only when certain conditions are met). But I need the chronometer to keep counting upward until the app and all its views are closed (I have an “Exit” function to do that).

The problem is that the Chronometer gets reset to zero on every time I look at another tab and come back to its activity. (This has to do with the oncreate, but I dont know the way around it)

I didn’t find an intuitive way to save the chronometer’s state or countup in the background on its own (or to perhaps keep track of the time on my own and update the chronometer visually at a different point in time)

One idea I had was to start the Chronometer with a service and let the service keep counting , while having a textview in the existing activity update using the chronometer’s current time tally as a string

any insight on a known approach to this problem be appreciated!

This is further complicated because this is an activity in a tabhost, and tabhosts call both onPause and onResume every time you load a view, so this breaks lifecycle functions.

  • 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-05-26T04:23:20+00:00Added an answer on May 26, 2026 at 4:23 am

    Because of my Tabhost, the lifecycle functions could not be relied on.

    What I did was make the chronometer a static global in a central class, and added a ontabchangedlistener within my tabhost that checked to see if the tab being changed to was the tab with the chronometer. If this was true then it stores the Long value of the chronometer’s current time.

     tabHost.setOnTabChangedListener(new OnTabChangeListener(){
    
            @Override
            public void onTabChanged(String arg0) {
                // TODO Auto-generated method stub
                if(arg0.contentEquals("homeGroup"))
                {
                    //store time in centralhelper.java
                                        //stopWatch is of type Chronometer
                                        //stopWatchLastTime is of type Long and is initially set to zero. Chronometer uses milliseconds to determine time, will never be zero after set
                    CentralHelper.stopWatchLastTime = CentralHelper.stopWatch.getBase();
                }
            }
    
        });
    

    When my homeGroup view loads, the onResume() function is called, there is a condition here to retrieve the time for the chronometer to resume counting from. Despite the fact that a tabhost will call both onPause() and onResume() in EVERY load outside of normal lifecycle functions, they still get called before onCreate()

       public void onResume(){
        super.onResume();
    
        //update Chronometer with time stored in tabchangelistener
        if(CentralHelper.stopWatchLastTime!=0)
            CentralHelper.stopWatch.setBase(CentralHelper.stopWatchLastTime);
    }
    

    this allowed me to do a similar check in onCreate()

        if(CentralHelper.stopWatchLastTime!=0)
        {
    
            CentralHelper.stopWatch.start(); //this is where it resumes counting from the base set in onResume()
        }
        else
        {
            CentralHelper.stopWatch.start();
            CentralHelper.stopWatch.setBase(SystemClock.elapsedRealtime());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that will be accessed from two threads: class Timer{ public:
I want to have a counter that counts down from 60 seconds to 0.
I currently have a application which will display the time allocated to a user
I have currently created a facebook like page that pulls notifications from different tables,
I have a timer that needs to not process its elapsed event handler at
I have a timer that ticks every 3 seconds. If the timer found something
I have an array an a timer that adds a new object to my
If I have an instance of a System.Timers.Timer that has a long interval -
If you have an ASP.NET UpdatePanel and and ASP.NET Timer that trigers theUpdatePanel to
I have a control that is basically functioning as a client-side timer countdown control.

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.