Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

未开始调用FlowCountDownTimer.start()之前切换Fragment导致调用start()时直接走already completed的判断 #2

Open
PostLiu opened this issue Dec 17, 2020 · 1 comment

Comments

@PostLiu
Copy link

PostLiu commented Dec 17, 2020

出现的情况:
采用ViewPager2加载2个不同的Fragment,其中一个Fragment中的点击调用倒计时工具,在未开始调用start前来回切换Fragment,调用倒计时导致直接走'already completed'的判断,其中倒计时的pause和resume方法分别放在Fragment的onPause和onResume方法中

    private fun sendCode(phone: String) {
        viewModel.sendCode(phone).catch {
            countDownTimer.cancel()
            binding.sendCode.text = "发送失败,重新发送"
            binding.sendCode.isEnabled = true
            toast(it.message.toString())
        }.asLiveDataUI(this, {
            toast("发送成功,请注意查看短信验证码")
            countDownTimer.start()
            countDownTimer.countDown {
                binding.sendCode.text = String.format(getString(R.string.next_send_code), it)
                binding.sendCode.isEnabled = false
            }.complete {
                binding.sendCode.text = "重新发送"
                binding.sendCode.isEnabled = true
                countDownTimer.cancel()
            }
        })
    }

    override fun onPause() {
        super.onPause()
        countDownTimer.pause()
    }

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

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer.cancel()
    }
@PostLiu PostLiu changed the title 未开始调用start()之前切换Fragment导致调用start()时直接走already completed的判断 未开始调用FlowCountDownTimer.start()之前切换Fragment导致调用start()时直接走already completed的判断 Dec 17, 2020
@PostLiu
Copy link
Author

PostLiu commented Dec 17, 2020

我真边给出的解决方法就是将 FlowCountDownTimer 中的判断

if (mJobState == JobState.COMPLETE) {
            Log.e(TAG, "already completed")
            return this
        }

给注释掉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant