Android 开发笔记 第二季 BACK STACK

BACK STACK ,以下简称 BS.


因为安卓不是单一入口的,所以应用可以启动第三发应用的 Activity 。

这种无缝的衔接,使得第三方应用就如同本应用的一部分一样。


安卓通过 BS 来管理 Activtiies 。

并不是说同一个应用产生的 Activities 会被归到一个 BS 中。


当你在主屏幕启动一个应用,那么就会创建一个BS,而最新出现的 Activity 则作为 根 Activity。

BS 是栈,先进后出,之后打开的 Activity 会不断被 push 进去。

在 BS 顶的 Activity 就是激活的,其他都是 stoped 的。

当你按 Back 回退时, Top Activity 会被销毁, 按照BS中的位置关系,会出现新的 Top Activity。

不断回退,指导 root Activity 被销毁,那么 BS 也随之销毁了。


还有这个么个概念,被激活的Activity所在的 BS,算是foreground。(其实没什么必要。)

当你按 Home 键,启动其他应用,产生新的 BS,那么就算原先的 BS background 了。

你再按 Home 键,重新点击之前的应用,那么原先 BS 激活,栈顶 Activity 被激活。


销毁问题

可以有多个 BS 处于background ,当系统觉得内存不够时,会销毁BS中除了 root Activity 的其他 Activity。 

系统会在记录下这些被销毁的在BS中的位置。 

因为当用户再次打开时,需要 recreate 他们。


SingleTask

默认情况下,一个 Activity 启动时会被实例化,

所以 Activity 可以实例化多次,处在多个BS中。

但 SingleTask 不同,复制原文:

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

 Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值