Android切换主题生命周期流程与onSaveInstanceState和onRestoreInstanceState,Kotlin

Android切换主题生命周期流程与onSaveInstanceState和onRestoreInstanceState,Kotlin

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val TAG = "fly"
    private val KEY = "my_key"
    private var mBundle: Bundle? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        val t = System.currentTimeMillis()
        outState.putString(KEY, t.toString())
        Log.d(TAG, "onSaveInstanceState ${t}")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)

        mBundle = savedInstanceState
        Log.d(TAG, "onRestoreInstanceState ${savedInstanceState.getString(KEY)}")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume ${mBundle?.getString(KEY)}")
    }

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

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

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

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

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

App启动后,把状态栏下滑出来,点击切换深色主题/暗色模式后的生命周期:

Android切换主题生命周期流程与onSaveInstanceState和onRestoreInstanceState,Kotlin_第1张图片

Android Activity生命周期以及onSaveInstanceState、onRestoreInstanceState要点备忘-CSDN博客本文作者:Zhang Phil原文链接:Android Activity生命周期以及onSaveInstanceState、onRestoreInstanceState要点备忘一般的,当Android activity的生命周期进入onPause后,Android系统紧接着就要回调:protected void onSaveInstanceStatehttps://blog.csdn.net/zhangphil/article/details/48155371Android Fragment生命周期图以及Activity与Fragment生命周期对照图-CSDN博客Android Fragment生命周期图以及Activity与Fragment生命周期对照图Android Fragment生命周期图:在Fragment的生命周期中,需要重点关注onCreate、onCreateView、onViewCreated。Android Activity与Fragment生命周期对比图:Activihttps://blog.csdn.net/zhangphil/article/details/49942843

你可能感兴趣的:(Android,kotlin,android,kotlin)