您的位置:首页 > 移动开发 > Android开发

Android--Activity

2013-11-04 00:00 633 查看
1,Android中什么是Task和Back Stack

Task可以理解为一个进程,启动一个应用就回启动一个进程,系统会为该应用分配16MB的空间来共该应用使用。Back Stack是以堆栈的形式管理一个应用启动的所有的Activity,每启动一个Activity都会把新的Activity放到栈顶,若栈顶Activity销毁(通过onBackPressed或者finish)之后,栈顶Activity下方的Activity会重新显示,栈的机制就是后进先出(last in, fisrt out)。



当一个Task中所有Activity都销毁时,这个Task则销毁,这个应用则关闭(除非还有一个Task启动了此应用)。

对于Android系统来说,后台可以运行多个Task任务,多个Task也是以堆栈行的进行管理。

先列出如下疑问,后续会逐一解答:

<1>如何使一个应用的所有Activity只在一个Task中?

<2>当应用A调用应用B的时候,应用B会在应用A的Task中还是会在新建的一个Task中?

<3>Android系统会在Activity被stopped之后保存这个Activity的状态(如输入框中内容,勾选状态等),当这个Activity被resume的时候,会显示被stopped之前的数据,但是有一种特殊情况:Android系统内存不足时,会将这个Activity被destory,这个Activity只能被create,之前的数据会出现丢失,如果想避免这种现象,可以在onSaveInstanceState()方法保存数据,在Activity被create或resume的时候将历史数据重新展示出来。

2,Activity的运行模式(launch mode)/启动方式:

<1>manifest配置式launchMode。

standard(默认值)--永久创建

singTop--只要栈顶不是这个Activity的实例就永久创建

Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in onNewIntent().

singTask--如果在一个Task中已经创建了这个Activity时,系统将不会再在这Task中创建这个Activity,而是通过onNewIntent()方法重新调用这个Activity。

Note: Although the activity starts in a new task, theBack button still returns the user to the previous activity.

singInstance--如果这个Activity已经存在,系统中所有的Task将公用这个Activity。

<2>Intent.FLAG方式,会覆盖配置式,优先级高于配置式。

FLAG_ACTIVITY_NEW_TASK:如果Activity将启动的Activity已经存在于Task中,则通过onNewIntent()直接将这个Activity移至栈顶,否则就会在当前Task创建这个Activity。等价于singTask

FLAG_ACTIVITY_SING_TOP:等价于singTop.

FLAG_ACTIVITActivityY_CLEAR_TOP:如果Task中这个Activity已经存在且不位于栈顶,则将栈中 位于这个Activity上方的所有Activity销毁,通过onNewIntent()将这个Activity移至栈顶。

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".

3,Mainfest中各个标签

Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".

Android系统一个特殊行为:当一个应用启动之后,长时间不操作,系统则会销毁这个应用所在的Task中的所有Activity除了root activity(这个activity是栈底activity还是启动activity有待确认),当返回这个应用时,则显示这个root activity。

设置root activity的属性alwaysRetainTaskState为true,上述特殊行为将不会执行,即保留这个应用所在的Task中的所有Activity。

设置root activity的属性clearTaskOnLaunch为true,无论离开应用多长时间,都销毁这个应用所在的Task中的所有Activity,进入应用初始状态,换句话说,clearTaskOnLaunch与alwaysRetainTaskState是相对的。

设置root activity的属性finishOnTaskLaunch为true,finishOnTaskLaunch与clearTaskOnLaunch优点相似,finishOnTaskLaunch应用于一个activity,而clearTaskOnLaunch应用于一个Task。

For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" .

上述内容仅为理论,有待实践。

摘自:http://developer.android.com/index.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android Task Stack Activity