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 7905843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:39:36+00:00 2026-06-03T10:39:36+00:00

I have been trying to setting up a function, which is getting the time

  • 0

I have been trying to setting up a function, which is getting the time since the window is appearing for the viewer.

I have been using a handler, timer, currenttimemillis.

That i have succeeded in doing. The window is showing the ongoing time since the window has been shown for the user – update frequency of 40fps, and i love how it looks and works..

BUT!

When going backwards and forwards into the window, the time restarts.
I want the window to show the (running) time since the initial view for the user and i don’t want the time “broken” even if the program is stopped.

I am using sharedpreferences for saving strings and i have tried to saving the initial System.currentTimeMillis(); as a sharedpreference (as a long) and loading it again on oncreate with this if statement


SharedPreferences sData14;
private final int REFRESH_RATE = 25;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orderwaiting); 
    setupVariables(); 
    sData14 = getSharedPreferences(filename, 0);

}

private void setupVariables() { 
    mHandler.removeCallbacks(startTimer);
    mHandler.postDelayed(startTimer, 0);

       if (sData14 == null) {
              starttime = System.currentTimeMillis();

             sData14 = getSharedPreferences(filename, 0); 
             SharedPreferences.Editor editor = sData14.edit();      
             editor.putLong("sharedString14", starttime);                       
             editor.commit();
           };

          if (sData14 != null) {
              Long value_long = sData14.getLong("sharedString14", 1000000);
              starttime = value_long;       
              }; 

EDITED ADDED

I don’t use onresume and onpause.
Do i have to do that ?

I would expect the if statement handles the “case”.. and i need a function that handles the case in which the program has been “closed”, but the function is still needed to track how far we have reached in time.

Is it better to doinbackground, and be able to call it from another place as well ?


It just doesn’t load the shared data…

In other words, i need a dynamic number to use from when the user first is viewing this window. Maybe there are other ways i could do this as well ??

I guess it should be working “this way”.. but i can’t seem to make it work..

I hope i have set up my problem in an simple way.


SOLUTION

long startTime;
private final int REFRESH_RATE = 25;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orderwaiting); 
setupVariables();     

}

private void setupVariables() { 

   SharedPreferences sData14 = getSharedPreferences(filename, 0);
   startTime = sData14.getLong("sharedString14", -1);
   **starttime = startTime;**

   if(startTime == -1){
       //startTime not previously saved
       starttime = System.currentTimeMillis();           

       SharedPreferences.Editor editor = sData14.edit();      
       editor.putLong("sharedString14", starttime);                       
       editor.commit();
   }

    mHandler.removeCallbacks(startTimer);
    mHandler.postDelayed(startTimer, 0);

i added the starttime = startTime, since it would else not load any starttime on the second view..
Thanks to David.. You saved my day 🙂

  • 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-03T10:39:38+00:00Added an answer on June 3, 2026 at 10:39 am

    Ah Ok, I missunderstood your initial question. The way you are checking your prefenences isn’t quite right, you can also simplify it quite a bit. The code below should fix your problems:

    long startTime;
    private final int REFRESH_RATE = 25;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.orderwaiting); 
    setupVariables();     
    
    }
    
    private void setupVariables() { 
    
       SharedPreferences sData14 = getSharedPreferences(filename, 0);
       startTime = sData14.getLong("sharedString14", -1);
    
       if(startTime == -1){
           //startTime not previously saved
           starttime = System.currentTimeMillis();           
    
           SharedPreferences.Editor editor = sData14.edit();      
           editor.putLong("sharedString14", starttime);                       
           editor.commit();
       }
    
        mHandler.removeCallbacks(startTimer);
        mHandler.postDelayed(startTimer, 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write a bare-bones ping scanner using Perl for internal
For a long time i have been trying to figure out what is the
Have have been trying to make a validator for my xml files. I have
I have been trying very hard to achieve rounded corners with IE6+jquery ui tabs.
I have been trying to us CALayers as sprites in an iPhone application I'm
I have been trying all afternoon to get the jQuery Sifr Plugin ( http://jquery.thewikies.com/sifr/
I have been trying to find a code snippet to do an unsharp mask
I have been trying to understand the way ActionScript's events are implemented, but I'm
I have been trying to find out why the following lines of code do
I have been trying to grasp the basics of Support Vector Machines, and downloaded

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.