基本生命周期

November 6, 2019

设备:Samsung Galaxy S4 系统:Android 5.0.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.i("MainActivity", "onCreate")
    }

    override fun onStart() {
        super.onStart()
        Log.i("MainActivity", "onStart")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.i("MainActivity", "onRestoreInstanceState")
    }

    override fun onResume() {
        super.onResume()
        Log.i("MainActivity", "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.i("MainActivity", "onPause")
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        Log.i("MainActivity", "onSaveInstanceState")
    }

    override fun onStop() {
        super.onStop()
        Log.i("MainActivity", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.i("MainActivity", "onDestroy")
    }
}

启动进入前台

1
2
3
com.phantomvk.playground I/MainActivity: onCreate
com.phantomvk.playground I/MainActivity: onStart
com.phantomvk.playground I/MainActivity: onResume

按Home键

1
2
3
com.phantomvk.playground I/MainActivity: onPause
com.phantomvk.playground I/MainActivity: onSaveInstanceState
com.phantomvk.playground I/MainActivity: onStop

点击图标回到前台

1
2
com.phantomvk.playground I/MainActivity: onStart
com.phantomvk.playground I/MainActivity: onResume

竖屏切换到横屏、横屏回到竖屏

1
2
3
4
5
6
7
8
9
com.phantomvk.playground I/MainActivity: onPause
com.phantomvk.playground I/MainActivity: onSaveInstanceState
com.phantomvk.playground I/MainActivity: onStop
com.phantomvk.playground I/MainActivity: onDestroy

com.phantomvk.playground I/MainActivity: onCreate
com.phantomvk.playground I/MainActivity: onStart
com.phantomvk.playground I/MainActivity: onRestoreInstanceState
com.phantomvk.playground I/MainActivity: onResume

前台点击返回键

1
2
3
com.phantomvk.playground I/MainActivity: onPause
com.phantomvk.playground I/MainActivity: onStop
com.phantomvk.playground I/MainActivity: onDestroy

电源键关闭屏幕

1
2
3
com.phantomvk.playground I/MainActivity: onPause
com.phantomvk.playground I/MainActivity: onSaveInstanceState
com.phantomvk.playground I/MainActivity: onStop

电源键解锁回到前台

1
2
com.phantomvk.playground I/MainActivity: onStart
com.phantomvk.playground I/MainActivity: onResume