Activity生命周期-Normal,Dialog

代码

  • Menifest:
<activity android:name=".cycle.MainLifeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".cycle.DialogLifeActivity" android:theme="@style/Theme.AppCompat.Dialog"/>
<activity android:name=".cycle.NormalLifeActivity"/>
  • Kotlin:

​MainLIfeActivity:​

class MainLifeActivity : AppCompatActivity() {
private val tag = "MainLife"

private lateinit var mainBinding: ActivityLifeMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(tag,"onCreate")
mainBinding = ActivityLifeMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)

mainBinding.mainStartNormal.setOnClickListener {
val intent = Intent(this, NormalLifeActivity::class.java)
startActivity(intent)
}
mainBinding.mainStartDialog.setOnClickListener {
val intent = Intent(this, DialogLifeActivity::class.java)
startActivity(intent)
}
}

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

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

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

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

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

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

​NormalLifeActivity,DialogLifeActivity:​

class NormalLifeActivity: AppCompatActivity() {
private lateinit var normalBinding: ActivityLifeNormalBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
normalBinding= ActivityLifeNormalBinding.inflate(layoutInflater)
setContentView(normalBinding.root)
}
}
class DialogLifeActivity: AppCompatActivity() {

private lateinit var dialogBinding: ActivityLifeDialogBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
dialogBinding= ActivityLifeDialogBinding.inflate(layoutInflater)
setContentView(dialogBinding.root)
}
}
  • Layout

​很简单,懒得贴了...​


效果

​启动:​

【Android】Activity生命周期(Normal,Dialog)_bundle



​​​点击Normal页面:​

【Android】Activity生命周期(Normal,Dialog)_android_02


​​​再返回:​

【Android】Activity生命周期(Normal,Dialog)_安卓_03


​点击Dialog页面:​

【Android】Activity生命周期(Normal,Dialog)_bundle_04


​​​再返回:​

【Android】Activity生命周期(Normal,Dialog)_android_05


​最后退出程序:​

【Android】Activity生命周期(Normal,Dialog)_ide_06
​​​删除后台:​

【Android】Activity生命周期(Normal,Dialog)_移动开发_07
【Android】Activity生命周期(Normal,Dialog)_安卓_08