Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 取消倒计时不';如果在onResume()中恢复计时器,则无法在onPause()中工作_Android_Kotlin_Countdowntimer_Onresume_Onpause - Fatal编程技术网

Android 取消倒计时不';如果在onResume()中恢复计时器,则无法在onPause()中工作

Android 取消倒计时不';如果在onResume()中恢复计时器,则无法在onPause()中工作,android,kotlin,countdowntimer,onresume,onpause,Android,Kotlin,Countdowntimer,Onresume,Onpause,我试图在onPause()中取消计时器,并在onResume()中恢复计时器,但取消不起作用。计时器仍在后台工作。有人知道为什么吗 var currentMillis: Long = 0 // First creation of your timer var timer = object : CountDownTimer(60000, 1000) { override fun onTick(millisUn

我试图在
onPause()
中取消计时器,并在
onResume()
中恢复计时器,但取消不起作用。计时器仍在后台工作。有人知道为什么吗

    var currentMillis: Long = 0

            // First creation of your timer
            var timer = object : CountDownTimer(60000, 1000) {
                override fun onTick(millisUntilFinished: Long) {

                    currentMillis = millisUntilFinished // <-- save value

                    textView3.text = (millisUntilFinished / 1000).toString() + ""
                    println("Timer  : " + millisUntilFinished / 1000)
                }

                override fun onFinish() {}
            }

        override fun onPause() {
                super.onPause()
                timer.cancel()


            }
            override fun onResume() {
                super.onResume()


                timer = object : CountDownTimer(currentMillis, 1000) {
                    override fun onTick(millisUntilFinished: Long) {
                        currentMillis = millisUntilFinished
                        textView3.text = (millisUntilFinished / 1000).toString() + ""
                        println("Timer  : " + millisUntilFinished / 1000)
                    }

                    override fun onFinish() {}
                }
                timer.start()

            }

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        setContentView(R.layout.activity_app)



timer.start()

timer=object:CountDownTimer
onResume()
中运行时,您正在删除一个现有引用:您在模块范围内创建的引用。即使您丢失了对它的引用,它仍会继续运行。它最终会得到GC'd,但直到倒计时结束。谢谢你的回答。使用
onResume()
是否有解决方法?您是否考虑过简单地消除模块作用域上的计时器?模块作用域是否为onResume()?如果我在简历中删除这个部分计时器,它将从60000毫秒开始。不,我指的是第一个计时器,而不是第二个计时器。
var i = 0
    if (i == 0) {
        timer = object : CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                currentMillis = millisUntilFinished
                textView3.text = (millisUntilFinished / 1000).toString() + ""
                println("Timer  : " + millisUntilFinished / 1000)
            }

            override fun onFinish() {}
        }
    }
    else
    {
        timer?.cancel()
        timer = object : CountDownTimer(currentMillis, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            currentMillis = millisUntilFinished
            textView3.text = (millisUntilFinished / 1000).toString() + ""
            println("Timer  : " + millisUntilFinished / 1000)
        }

        override fun onFinish() {}
    }
    }

    timer?.start()
    i++