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

76 Views Asked by At

My AndroidManifest.xml does not include anything that would cause this (that I know of).

I.e. it does not include:

android:configChanges="orientation"

Android Version: Chipmunk 2021.2.1 Patch 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()
    }
2

There are 2 best solutions below

0
Shenal Akalanka On

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);
0
Anthone On

Maybe be you don't activate rotation in the top panel of your simulator.