Android Dev GuideTasks and Back Stack(上)

Tasks and Back Stack

今天来整理一下Tasks and Back Stack,有时候还是感觉英文表达的意思更贴近一些,所以在Dev Guide中我感觉需要我们去理解的话我就不翻译成中文了,还是英文看起来比较流畅一些,当然这些都是非常通俗易懂的。

1,An activity can even start activities that exist in other applications on the device.

这句话告诉我们,我们建立的一个application,我们的application中包含很多的 activities,activity不仅能启动我们application中的activity,还可以启动其它应用中的activity。好下面我们来理解一下这个场景,假如我们的application是一个阅读程序,我们在阅读英文的时候有时候会遇到很生疏的单词,这时候我们希望在程序中就能打开我们设备中已经安装的翻译的程序。我们在之前跟翻译程序的开发商已经定义好接口,这样我们就能在我们的程序中去利用定义好的接口来直接调用翻译程序了。

2, Android maintains this seamless user experience by keeping both activities in the same task.

3,Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the Back button.

4,As such, the back stack operates as a "last in, first out" object structure.(后进先出的原则)

To summarize the default behavior for activities and tasks:

  • When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses the Back button while in Activity B, Activity A resumes with its state restored.
  • When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.
  • If the user presses the Back button, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.
  • Activities can be instantiated multiple times, even from other tasks.

Managing Tasks

对android的android:taskAffinity初识(来源于http://henzil.easymorse.com/?p=481)

android中AndroidManifest.xml文件,

activity标签中有一个属性为android:taskAffinity=“String”

如果不设置此属性,默认一个应用程序只有一个栈,这个栈以应用包命为单位。

下面是我做的三个实验:

1、新建两个工程,application01application02

application01设置 android:taskAffinity="aaa.aaa" android:allowTaskReparenting="true"

application02也设置 android:taskAffinity="aaa.aaa" android:allowTaskReparenting="true"

先运行application01,然后点击home键,让application01运行在后台,再运行application02,现在按返回键,当前显示的是application01mainActivity,再点返回键,回到home页。

但是长按home键,发现运行过的程序只有application01

2、紧接着又在此基础上做了另外一个实验,在application01上新建一个secondActivity,设置

android:taskAffinity="aaa.bbb" android:allowTaskReparenting="true"

mainActivity中startActivity时,设置Intent中flag属性为FLAG_ACTIVITY_NEW_TASK,注意,这里的flag属性必须为new task。

然后运行application01,点击进入secondActivity,点击home键,让程序回到后台,

然后运行application02,点击返回,当前显示的application01中的mainActivity,再点击返回,显示的是application01secondActivity,跟预期一致。

3、在此基础上堆application01再次修改,增加thirdActivity,设置属性android:taskAffinity="aaa.aaa" android:allowTaskReparenting="true"

并在secondActivity中startActivity时,设置Intent中flag属性为FLAG_ACTIVITY_NEW_TASK

运行application01,点击进入secondActivity,再进入thirdActivity,点击返回,回到mainActivity,在点击返回,回到secondActivity,再点击返回,回到home页面,跟预期一致。

相信我们按照上面的操作做完之后对这个属性会有一定的了解,它的实质就是指定某些application或者是其中的activity是否共用一个task,都遵循后进先出的原则

这篇好像什么都没整理,但是点点滴滴才能汇聚成河,在下一篇Android Dev Guide---Tasks and Back Stack(下)中我们介绍activity的四种加载模式。

原文地址:https://www.cnblogs.com/oasis2008/p/2355907.html