简体   繁体   中英

onPause, onStop, onDestroy not stopping timer

In my onCreate method in my activity i call a method from an object and pass the methods value as 1 which means to start a timer in the objects class. However I want to stop the timer whenever the app closes, loses focus or someone pressed the back button on their device and exited the app. I tried doing this below my onCreate method with an onPause, onStop, onDestroy and entered the methods value as 2 for the object which means to cancel the timer. However my problem is that whenever someone presses the back button on their device and then goes back in to the app the same timer is running twice because the app did not cancel the timer in the onStop, onPause or onDestroy. Why didn't the onStop, onPause and onDestroy stop the timer and how do i make it stop the timer so two arent running when the app is reopened?

Activity below

Ship mShip = new Ship(0,0,0);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

        mShip.timerStart(1);

}


@Override
public void onPause()
{
    super.onPause();
    mShip.timerStart(2);
}
@Override
public void onStop()
{
    super.onStop();
    mShip.timerStart(2);
}
@Override
public void onDestroy()
{
    super.onDestroy();
    mShip.timerStart(2);
}

Ship Class below

    public static int counter = 0;
    public static int counterPerSec = 5;

TimerClass startTimer = (TimerClass) new TimerClass(2000,1000)
    {
        @Override
        public void onFinish() {
           counter += counterPerSec;
            this.start();
        }
    };


    public void timerStart(int x) {

        if(x == 1)
        {
           startTimer.start();
        }

        if(x == 2)
        {
           startTimer.cancel();
        }

    }

Timer Class

public class TimerClass extends CountDownTimer {
public TimerClass(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
}


@Override  // when timer is finished
public void onFinish() {
    this.start();

}
@Override  // on every tick of the timer
public void onTick(long millisUntilFinished) {



}

}

好吧,我想我可以正确地假设onPause,onStop和onDestroy正在执行,因此我敢猜测您的TimerClass类中存在错误。

I can not see, why your timer is not canceled. But there is another bug in your code: You can not pause and resume a countdown timer by calling resume and start.

If your time gets canceled, you should save the old timer vaules. And if your timer has to be resumed, you can create a new timer with the old timer values. See: Android: How to pause and resume a Count Down Timer?

To your question: Can you debug and check if onPause, onStop, onDestroy is called? Is there any exception thrown? Do you have any compile warnings?

Last important question: How do you know that two timers are running?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM