kotlinandroid-activityonsaveinstancestate

I have a problem with Simple Android Activity Lifecycle


Could you explain me, why both of my Android 12 and Android 8 does not call method onSaveInstanceState()

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater).also { setContentView(it.root) }
        Log.d("AAAAA", "onCreate")

        binding.buttonSet.setOnClickListener {
            binding.tvData.text = "This is my meesage"
        }

    }

    override fun onStart() {
        Log.d("AAAAA", "onStart")
        super.onStart()
    }

    override fun onResume() {
        Log.d("AAAAA", "onResume")
        super.onResume()
    }

    override fun onPause() {
        Log.d("AAAAA", "onPause")
        super.onPause()
    }

    override fun onStop() {
        Log.d("AAAAA", "onStop")
        super.onStop()
    }

    override fun onDestroy() {
        Log.d("AAAAA", "onDestroy")
        super.onDestroy()
    }

    override fun onRestart() {
        Log.d("AAAAA", "onRestart")
        super.onRestart()
    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.d("AAAAA", "onSaveInstanceState")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d("AAAAA", "onRestoreInstanceState")
    }


}

My Logcat:

2022-07-16 11:37:30.344 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onCreate 2022-07-16 11:37:30.346 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStart 2022-07-16 11:37:30.346 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onRestoreInstanceState 2022-07-16 11:37:30.347 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onResume 2022-07-16 11:37:31.458 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onPause 2022-07-16 11:37:31.460 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStop 2022-07-16 11:37:31.461 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onDestroy 2022-07-16 11:37:31.518 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onCreate 2022-07-16 11:37:31.519 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStart 2022-07-16 11:37:31.520 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onRestoreInstanceState 2022-07-16 11:37:31.520 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onResume 2022-07-16 11:40:26.012 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onPause 2022-07-16 11:40:26.015 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStop 2022-07-16 11:40:26.016 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onDestroy 2022-07-16 11:40:26.076 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onCreate 2022-07-16 11:40:26.078 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStart 2022-07-16 11:40:26.078 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onRestoreInstanceState 2022-07-16 11:40:26.079 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onResume 2022-07-16 11:40:27.013 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onPause 2022-07-16 11:40:27.014 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStop 2022-07-16 11:40:27.015 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onDestroy 2022-07-16 11:40:27.067 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onCreate 2022-07-16 11:40:27.069 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onStart 2022-07-16 11:40:27.070 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onRestoreInstanceState 2022-07-16 11:40:27.070 29235-29235/com.elene.activitylifecycle23 D/AAAAA: onResume

My Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.elene.activitylifecycle23">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ActivityLifecycle23"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.636" />

</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • It was a problem with onSaveInstanceState() method. In this occasion we should use onSaveInstanceState(outState: Bundle)