0

I currently have a tapping game which utilises the on pause, stop and onresume methods when the user presses the home button, the timer is 'cancelled' and the time left saved as long and then in the onresume a new timer is created with the time remaining - fine, however now when the timer completes a full run and the user procedes to start a new activity i.e. leaderboard/settings etc.. and returns the onResume is fired again and a countdown with 1 second remaining begins again, is there a way of stopping it being fired when returning from a certain activity or similar?

below you'll see a bodge job i have tried in vain to use, very messy and in effective but im running out of ideas!

  protected void onResume() {
        super.onResume();

        if (timeLeft < 1000) {

        } else {
            if (timeLeft <= 0) {

            } else {

                if (newActivityOpened == true) {

                } else {
                    if (isTimerRunning == true) {
                        startCountDownTimer();
                    }

                }

            }
        }
    }

Excuse all the redundant if statements, i keep adding them hoping to make something click haha.

Broak
  • 4,161
  • 4
  • 31
  • 54

3 Answers3

1

Surely all you need to do is the following...

// Code where you start the new Activity...
stopCountDownTimer();
if (timeLeft < 1000)
    isTimerRunning = false;
startActivity(intent);

Then in onResume() you just do...

if (isTimerRunning)
    startCountDownTimer();
Squonk
  • 48,735
  • 19
  • 103
  • 135
0

Why don't u use a simple global boolean in your activity? Set it to true whenever you start some activity. In your onResume() check for the boolean. If you see its set to true then don't start the countdown timer. otherwise start it.

protected void onResume() { 
    if( !newActivityOpened){
        if ( isTimerRunning ) {
            startCountDownTimer();
        }
    }
    super.onResume();
}

Not sure if I got ure problem correctly, but this should get it working.

Shubhayu
  • 13,402
  • 5
  • 33
  • 30
0

Just create a flag that indicates if you are currently timing/counting down. Once you start your timer set timing = true; once it completes timing = false;

Then in your onResume you know whether or not your timer has already started running.

protected void onResume() {
    if (timing)
    {
        //  create your timer here
    }
    else{
        // do nothing?
    }
}
dymmeh
  • 22,247
  • 5
  • 53
  • 60