Patroy
Patroy

Reputation: 377

Activity with countDown timer pops up after time is up, how to kill it?

I have kind of a game with 2 activities: Start Activity (with high score, start button and tutorial button) and main game activity which is is based on countdown timer, when time is up, game returns to start activity.

Problem is when user starts game and then hits home button and goes back to home screen (just leaving game by home button) Everything is ok until time is up, then Start activity appears on screen with lost message.

I've tried various combined methods like onPause witch finish(); inside and so on but it doesn't work or causes app force close.

I can't handle home button click like in onBackPressed() which I did in that case.

Is there any way to suspend app and pause all threads while it isn't in foreground?

Upvotes: 0

Views: 102

Answers (1)

Radesh
Radesh

Reputation: 13565

I suggest two way

1

Create global variable like

private isInForgrand = false;

And in onStop() or onPause() and onResume() change it

@Override
public void onStop() {
    isInForgrand = false;
    super.onStop();
}
@Override
public void onResume() {
    super.onResume();
    isInForgrand = true;
}

And in onFinish() check it

@Override
        public void onFinish() {
            if(isInForgrand){
               //do what you want  
             }else{
               //your app NOT in Forgrannd
        }

2

You can cancel CountDownTimer in onStop()

@Override
public void onStop() { 
    super.onStop();
    mCountDownTimer.cancel();
}

Upvotes: 1

Related Questions