Android的四种启动模式

一、Activity的四种启动模式:
1、standard(标准模式):一个定义Activity在mainfest中不设置Android:launchMode=“standard”,也会默认为standard,standard就是新建一个Activity就在栈中新建一个Activity实例 。
2、singleTop(栈顶复用模式):在mainfest中设置singleTop模式时,当前栈顶如果有一个相同的Activity,就不创建而复用栈顶的那个,只要创建新的和栈顶相同的Activity才会复用,复用的Activity就回调onNewIntent方法 。
3、singleTask(栈内单例模式):当前栈内只有一个Activity实例,栈内已存activity实例,在其他Activity中开启这个Activity,Android直接把这个实例栈上面的其他Activity实例踢出栈GC掉 。
4、singleInstace(堆内单例):设置该模式的Activity实例存在一个单独的任务栈中,整个系统独立的 。
设置了singleTop、singleTask、singleInstance这三种模式的Activity,如果开启一个新的Activity页面,栈顶存在相同的实例就复用,都不会重新创建一个新实例,Activity复用后都会调用onNewIntent(Intent intent)方法 。
测试demo例子:
在mainfest清单文件中定义这些Activity
<activity android:name=".lauchmode.LaunchModeActivity"android:launchMode="singleTask"/><activityandroid:name=".lauchmode.StandardActivity"android:launchMode="standard" /><activityandroid:name=".lauchmode.SingleTopActivity"android:launchMode="singleTop" /><activityandroid:name=".lauchmode.SingleTaskActivity"android:launchMode="singleTask"/><activity android:name=".lauchmode.SingleInstanceActivity"android:launchMode="singleInstance"/>class LaunchModeActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityLaunchModeBinding.inflate(layoutInflater)setContentView(binding.root)binding.btStandard.setOnClickListener {startActivity(Intent(this, StandardActivity::class.JAVA))}binding.btSingleTop.setOnClickListener {startActivity(Intent(this, SingleTopActivity::class.java))}binding.btSingleTask.setOnClickListener {startActivity(Intent(this, SingleTaskActivity::class.java))}}}以下就是LaunchModeActivity页面,下面按钮分别跳转到StandardActivity 、SingleTopActivity、SingleInstanceActivity、SingleTaskActivity页面 。

Android的四种启动模式

文章插图
Javaclass StandardActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityStandardBinding.inflate(layoutInflater)setContentView(binding.root)binding.btStandard.setOnClickListener {startActivity(Intent(this, StandardActivity::class.java))}binding.btSingleTop.setOnClickListener {startActivity(Intent(this, SingleTopActivity::class.java))}binding.btSingleTask.setOnClickListener {startActivity(Intent(this, SingleTaskActivity::class.java))}binding.btSingleInstance.setOnClickListener {startActivity(Intent(this, SingleInstanceActivity::class.java))}}}class SingleTopActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivitySingleTopBinding.inflate(layoutInflater)setContentView(binding.root)binding.btStandard.setOnClickListener {startActivity(Intent(this, StandardActivity::class.java))}binding.btSingleTop.setOnClickListener {startActivity(Intent(this, SingleTopActivity::class.java))}binding.btSingleTask.setOnClickListener {startActivity(Intent(this, SingleTaskActivity::class.java))}binding.btSingleInstance.setOnClickListener {startActivity(Intent(this, SingleInstanceActivity::class.java))}}}class SingleTaskActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivitySingleTaskBinding.inflate(layoutInflater)setContentView(binding.root)binding.btStandard.setOnClickListener {startActivity(Intent(this, StandardActivity::class.java))}binding.btSingleTop.setOnClickListener {startActivity(Intent(this, SingleTopActivity::class.java))}binding.btSingleTask.setOnClickListener {startActivity(Intent(this, SingleTaskActivity::class.java))}binding.btSingleInstance.setOnClickListener {startActivity(Intent(this, SingleInstanceActivity::class.java))}}}class SingleInstanceActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivitySingleInstanceBinding.inflate(layoutInflater)setContentView(binding.root)binding.btStandard.setOnClickListener {startActivity(Intent(this, StandardActivity::class.java))}binding.btSingleTop.setOnClickListener {startActivity(Intent(this, SingleTopActivity::class.java))}binding.btSingleTask.setOnClickListener {startActivity(Intent(this, SingleTaskActivity::class.java))}binding.btSingleInstance.setOnClickListener {startActivity(Intent(this, SingleInstanceActivity::class.java))}}}


推荐阅读