Activity的四种启动模式和应用场景

本文详细介绍了Android中Activity的四种启动模式:standard、singleTop、singleTask和singleInstance,通过实例演示了每种模式的特点和使用场景,并提供了转换为图形的说明,帮助开发者更好地理解和运用这些模式。
摘要由CSDN通过智能技术生成

Activity的四种启动模式和应用场景

简介

通过设置ActivityManifestActivity_launchMode可以设置Activity的启动模式。

默认情况下,使用启动模式:standard

同时,launchMode可以通过Intent flags的改变在运行时被复写,比
如 Intent flags FLAG_ACTIVITY_SINGLE_TOPFLAG_ACTIVITY_NEW_TASK,
FLAG_ACTIVITY_MULTIPLE_TASK.

四种模式

Constant

Value

Description

standard

0

默认模式,会在启动时创建一个新实例,创建的模式也可以随Intent.FLAG_ACTIVITY_NEW_TASK而改变

singleTop

1

当启动activity时,有相同的activity在前台与用户交互,那就复用这个activity,这个实例会被调用Activity.onNewIntent()

singleTask

2

在启动activity时,若有一个运行着这个activitytask,那这个activity实例会被调到前台,并调用Activity.onNewIntent() ,启动实例的Intentflag会被设置Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT . singleTasksingleTop的一个扩展集。This is a superset of the singleTop mode, where if there is already an instance of the activity being started at the top of the stack, it will receive the Intent as described there (without the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set). See the Tasks and Back Stack document for more details about tasks.

singleInstance

3

开辟一个只允许一个activity实例在里头运行的task. 如果用同样的intent再次启动这个activitytask会被调到前台,其Activity.onNewIntent() 会被调用. 如果这个activity实例要启动一个新activity,那么这个新activity会在一个新task中运行.

Demo范例调试

目录结构:

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值