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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:21:15+00:00 2026-05-26T23:21:15+00:00

I have a puzzle game for android. When the puzzle starts I take the

  • 0

I have a puzzle game for android. When the puzzle starts I take the current time:

long startTime = System.currentTimeInMillis()

When the player completes the puzzle, I take the time again, subtract the starting time and work out the elapsed time. This is all ok.

My problem is what to do when the application is interrupted. For example by a phone call. At the moment, the puzzle remains in it’s previous state automatically (as it is in a view). However, the calculation completionTime = currentTime - startTime will now be invalid.

I have tried saving the elapsed time using onSaveInstaceState(Bundle). However its counterpart, onRestoreInstanceState(Bundle) is not called when re-entering the app. Rather, the onResume() method is called instead? I have read this is because the app has not been ‘killed’, rather it is still in memory. In the case of a ‘kill’, I’d imagine the state of the View will also be lost? I don’t think it’s terribly necessary to keep track of the view in this case, so I won’t worry about the time either.

Is there a way to read a bundle from onResume(), should I just implement a shared preference?

I’d like to avoid updating the elapsed time in the game loop as this seems inefficient.

  • 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-26T23:21:16+00:00Added an answer on May 26, 2026 at 11:21 pm

    I would advice not using a SharedPreference at all.


    You will only need 2 fields: startTime and elapsedTime

    When the player starts, initialise elapsedTime to 0 and startTime to System.currentTimeMillis()

    When onPause() is called, initialise elapsedTime using

    elapsedTime = elapsedTime + (System.currentTimeMillis() - startTime);
    

    When onResume() is called, initialise startTime to System.currentTimeMillis()

    When the player is done, the time is

    elapsedTime = elapsedTime + (System.currentTimeMillis() - startTime);
    

    Please, if the logic has a flaw, comment (:

    Note: There exists a way to use only one field.! But we’ll keep that for the reader to discover.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple puzzle game. There is an image consisting of 16 tiles
I have a coin MovieClip which revolves around different orbits in my puzzle game.
if (win) { // Game was won, set completed in puzzle and time //
I want to make a puzzle game in XNA where I take a picture
I'm currently working on a 15 puzzle game for Android for school. Sadly I've
This is a bit of a puzzle - I have created a small game
I have simple 2D puzzle game earlier I created it extending view and everything
I have a puzzle game where pieces are dragged around the screen but can
I am writing a word puzzle game. I want to have a plist file
I have implemented a version of Rush Hour (the puzzle board game) in Python

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.