5 Replies - 420 Views - Last Post: 26 December 2013 - 04:33 PM

#1 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Pause time onStop onPause

Posted 17 December 2013 - 06:51 PM

In my puzzle application, I keep track of elapsed time to solve the puzzle.

I do so by grabbing system time at puzzle start, and subtract it from system time at puzzle completion.

My problem is that I can't "pause" the "timer" when I minimize game. When I restore game and complete puzzle, it gives elapsed time including time while minimized.

Obviously system time is always elapsing.

So I thought I could just use onpause and onstop methods to save time value, since your program variables are not detroyed unless onDestroy is called, which is not the case when you minimize.

So I implemented the following code. Time is an instance variable in my activity class.

When minimized get elapsed time -> (systemTime-StartTime) and save it.

When maximized get System time and set it backwards the previous elapsed amount (systemTime-savedTime).

It doesn't seem to work at all, and it seems like these lines of code are not being executed.


@Override
	public void onstart()
	{
		
		super.onstart();
		time=System.currentTimeMillis()-time;
		setOptions();
		
	}
	
	@Override
	public void onstop()
	{
		
		super.onstop();
		time=System.currentTimeMillis()-time;
		
	}
	
	@Override
	public void onpause()
	{
		
		super.onpause();
		time=System.currentTimeMillis()-time;
		
	}
	
	@Override
	public void onresume()
	{
		super.onresume();
		time=System.currentTimeMillis()-time;
		setOptions();
	}




Is This A Good Question/Topic? 0
  • +

Replies To: Pause time onStop onPause

#2 Apokio  Icon User is online

  • D.I.C Addict

Reputation: 135
  • Posts: 511
  • Joined: 14-August 09

Re: Pause time onStop onPause

Posted 18 December 2013 - 09:39 AM

You need to use Shared Preferences to store information to be picked back up after the application stops. You can't use a regular variable because it loses it value during the onpause or onstop. Here is a link to SharedPreferences.
Was This Post Helpful? 0
  • +
  • -

#3 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: Pause time onStop onPause

Posted 18 December 2013 - 09:25 PM

I used sharedpref to solve my problem, but I am still really confused.

Before I ever saved any values, the variable time was saved. I would minimize app, and maximize, and upon completing puzzle, it would give me the elapsed time including the time minimized. The only way that was possible is if it was already saving the integer time.
Was This Post Helpful? 0
  • +
  • -

#4 Apokio  Icon User is online

  • D.I.C Addict

Reputation: 135
  • Posts: 511
  • Joined: 14-August 09

Re: Pause time onStop onPause

Posted 19 December 2013 - 05:54 AM

Your app will save some things in memory automatically, but if the app gets forced closed for any reason then those values are lost. I had this problem with my app on phones with low RAM. Id Android needs more memory it will free up space. Using something like Shared Preferences tells android that it needs to save that data no matter what. At least that is how I understand it. Read up some more on the activity life cycle, it took me a while to understand it and every little thing that happens. Heck, I am still learning and trying to keep up with the new stuff.
Was This Post Helpful? 0
  • +
  • -

#5 Huskerfan  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 31-January 12

Re: Pause time onStop onPause

Posted 25 December 2013 - 04:11 PM

The shared preference approach makes perfect sense if you are also persisting the state of your game throughout destroys and device reboots. If you are not doing this, then there is no reason to keep the timer value(s) in a shared preference, because if your app is killed for whatever reason, you won't have a game state to return to, so at that point you no longer need the elapsed time.

Regardless, you should try to consider only storing what you need, which is the last time the game was started (either from cold start or from onresume, and the total elapsed time.

Consider something like this:
@Override
public void onresume() { // 'r' in resume should be capitalized... GG DIC [code] tags
    mStartTime = System.currentTimeMillis();
}

@Override
public void onpause() { // 'p' in pause should be capitalized... GG DIC [code] tags
    mElapsedTime = mElapsedTime + (System.currentTimeMillis() - mStartTime);
}



This is obviously assuming mStartTime and mElapsedTime are long member variables of your activity
Was This Post Helpful? 0
  • +
  • -

#6 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: Pause time onStop onPause

Posted 26 December 2013 - 04:33 PM

I understand everything that is being said here. But what I don't understand is this.

I have an instance variable long time in my main activity.

And this happened...

//VERSION 1
@Override
public void onpause()
{
     super.onpause();
     //Nothing is done whatsoever with the variable "time"
}

@Override
public void onresume()
{
     super.onresume();
     //on resume, variable time is same value as when paused
}

//VERSION 2
@Override
public void onpause()
{

     super.onpause();
     time=System.currentTimeMillis()-time;
     //Time is modified in onpause
}

@Override
public void onresume()
{
    
     super.onresume();

     //on resume, variable time is set at zero
}




So it seemed like time would be properly stored, unless I tried to change it in onpause(). Its not like it is a problem anymore, the shared pref solution works fine. I am just curious here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1