You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

App旋转后提前结束及计时器未保存问题的技术咨询

Fixes for Your Device Rotation Issues in Android

Hey there! Let’s tackle these two frustrating rotation-related problems one by one—they’re both super common when dealing with Android’s lifecycle during configuration changes, so I’ve got actionable solutions for you.

1. App Jumps to Result Page After One Question Post-Rotation

What’s Likely Happening

When you rotate your device, the Activity/Fragment gets destroyed and recreated by default. If you aren’t saving critical state like your current question index or whether the current question was already answered, your app might be resetting to a state where it incorrectly thinks all questions are finished.

How to Fix It

  • Save key state in onSaveInstanceState: Capture values that track your progress, like the current question position and whether you’ve submitted an answer for that question.
  • Restore state when the component recreates: Pull those saved values back in onCreate or onViewStateRestored instead of reinitializing everything from scratch.

Example code (Kotlin):

private var currentQuestionIndex = 0
private var isCurrentQuestionAnswered = false

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    // Save your progress state
    outState.putInt("currentQuestionIndex", currentQuestionIndex)
    outState.putBoolean("isCurrentQuestionAnswered", isCurrentQuestionAnswered)
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quiz)

    if (savedInstanceState != null) {
        // Restore state if we're recreating after rotation
        currentQuestionIndex = savedInstanceState.getInt("currentQuestionIndex")
        isCurrentQuestionAnswered = savedInstanceState.getBoolean("isCurrentQuestionAnswered")
    } else {
        // Initialize fresh state for first launch
        currentQuestionIndex = 0
        isCurrentQuestionAnswered = false
    }

    // Load the correct question based on restored index
    loadCurrentQuestion()
}

Also, double-check your navigation logic that triggers the result page. Make sure it’s only navigating when currentQuestionIndex exceeds the total number of questions, not just on a default reset value.

2. Countdown Timer Resets to 59 Seconds on Rotation

What’s Likely Happening

Your previous putInt("time", times) attempt might have had issues with how you were restoring the value, or you weren’t properly canceling/restarting the timer with the saved remaining time. Also, using Int might be problematic if you’re tracking milliseconds (since 60 seconds is 60000 ms, which fits in an Int, but using Long is safer for time values).

How to Fix It

  • Save the remaining time as a Long (to handle millisecond precision):
  • Cancel the existing timer before saving state, then restart it with the restored time when the component recreates.

Example code (Kotlin):

private var countDownTimer: CountDownTimer? = null
private var remainingTimeMillis: Long = 60000 // 60 seconds in milliseconds

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    // Cancel the timer to stop it mid-tick
    countDownTimer?.cancel()
    // Save the remaining time
    outState.putLong("remainingTimeMillis", remainingTimeMillis)
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_quiz)

    if (savedInstanceState != null) {
        // Restore the saved remaining time
        remainingTimeMillis = savedInstanceState.getLong("remainingTimeMillis")
    } else {
        // Initialize to 60 seconds on first launch
        remainingTimeMillis = 60000
    }

    // Start the timer with the correct time
    startCountdownTimer(remainingTimeMillis)
}

private fun startCountdownTimer(timeInMillis: Long) {
    countDownTimer = object : CountDownTimer(timeInMillis, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            remainingTimeMillis = millisUntilFinished
            // Update your UI with the remaining time
            binding.tvCountdown.text = "${millisUntilFinished / 1000} seconds"
        }

        override fun onFinish() {
            // Handle timer end logic (e.g., auto-submit question)
            binding.tvCountdown.text = "Time's up!"
        }
    }.start()
}

A quick note: If you’ve added android:configChanges="orientation|screenSize" to your Activity in the manifest, onSaveInstanceState won’t be called automatically. In that case, you’d need to handle state saving/restoring in onConfigurationChanged instead—but the above approach using onSaveInstanceState is the recommended, lifecycle-friendly way.

内容的提问来源于stack exchange,提问作者here2learn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:06:01