简体   繁体   English

为什么旋转 AVD 时不调用 Android onCreate() 方法?

[英]Why is the Android onCreate() method not called when the AVD is rotated?

My AndroidManifest.xml does not include anything that would cause this (that I know of).我的 AndroidManifest.xml 不包含任何会导致此问题的内容(据我所知)。

Ie it does not include:即它不包括:

android:configChanges="orientation" android:configChanges="方向"

Android Version: Chipmunk 2021.2.1 Patch 1安卓版本:Chipmunk 2021.2.1 补丁 1

class MainActivity : AppCompatActivity() {
    internal lateinit var tapMeButton: Button
    internal lateinit var gameScoreTextView: TextView
    internal lateinit var timeLeftTextView: TextView
    internal var score: Int = 0
    internal var gameStarted = false
    internal lateinit var countDownTimer: CountDownTimer
    internal val initialCountDown: Long = 5000
    internal val countDownInterval: Long = 1000

    companion object {
        private val TAG = MainActivity::class.java.simpleName
    }

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

        Log.d(TAG, "onCreate called. Score is $score")

        tapMeButton = findViewById(R.id.tapMeButton)
        gameScoreTextView = findViewById(R.id.gameScoreTextView)
        timeLeftTextView = findViewById(R.id.timeLeftTextView)

        tapMeButton.setOnClickListener { view ->
            if (!gameStarted) {
                startGame()
            }
            incrementScore()
        }

        resetGame()
    }

It's hard to help you without seeing your code.没有看到你的代码很难帮助你。 But check that is this line available in your code.但请检查您的代码中是否有此行可用。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

也许您没有在模拟器的顶部面板中激活旋转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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