zoukankan      html  css  js  c++  java
  • Tasks and Back Stack

    链接:https://developer.android.com/guide/components/tasks-and-back-stack.html

    Task是Activity的集合,按打开的先后顺序存放在栈中。

    Home界面是启动绝大多数Task的地方,用户点击应用的icon后,应用的task就会被呼叫到前台,如果之前没有运行过该应用,就会为该应用创建一个新的Task,

    并以应用的MainActivity作为栈的根Activity。

    当A(activity)启动B(activity)时,B就会加入到A的Task中,A会被停止,系统会保存A的状态。此时,用户再按返回键,B出栈,被销毁,A会被恢复。如下图:

    Task只剩下最后一个activity时,用户再按返回键,就会回到Home界面,Task也就不存在了。

    Task可以作为一个整体移动到后台。用户启动A应用,打开了3个界面,表示TaskA被创建,里面有3个activity,此时用户按Home键,TaskA里面的activity就全部被stop,

    但是TaskA仍然是完整的,只是失去了焦点。Android支持多后台任务,用户可以在多个Task之间切换。

    Note: Multiple tasks can be held in the background at once. However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. See the following section about Activity state.

    注意,后台activity可能被回收。

    默认的Task和activity行为总结:

    • 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.

    Saving Activity State

    see the Activities document.......

    Managing Tasks

    In the following sections, you'll see how you can use these manifest attributes and intent flags to define how activities are associated with tasks and how they behave in the back stack.

    Defining launch modes

    设置启动模式,有两种方式:

    Using the manifest file

    使用<activity>节点的 launchMode 属性,有四种属性值:

      "standard" (the default mode)

        每次启动时,创建一个实例,一个任务栈可以有多个实例,每个实例可以属于不同的任务栈。

      "singleTop"

        如果这个activity已经存在于当前任务栈的顶部了,那么这个activity的onNewIntent()方法会被触发,不会新创建实例。一个任务栈可以有多个实例(顶部的activity不是当前activity的实例),

        每个实例可以属于不同的任务栈。

        举个例子,一个任务栈由根activityA和activityB,C,D组成,D在最上面。如果D是standard模式,此时再启动D,任务栈由A-B-C-D变为A-B-C-D-D,如果B是singleTop模式,任务栈不变,

        D的onNewIntent()触发,此时用户接着按返回键,D被出栈销毁。

      "singleTask"

        系统创建一个新的Task,实例化这个activity作为Task的根activity。如果activity的实例已经存在,该实例的onNewIntent()方法被触发,不会新建。同时,只能存在一个该实例。

      "singleInstance"

        该activity只能在一个单独的Task中,改activity实例对象是Task的唯一成员。

      举个例子:在应用中,启动一个浏览器(浏览器的activity是singleTask的),这就意味着,你启动的浏览器activity并不会放到当前任务栈中,而是新开一个任务栈,如果浏览器activity已存在,会触发

      onNewIntent()方法。

      不管新启动的activity是在当前任务栈还是新启动一个任务栈,按后退键都会返回到上一个activity。但是也有特例:

      如果你启动的activity是singleTask模式,并且这个activity还在一个后台栈中,那么整个后台栈都会被移动到前台,后退栈就包含了所有的activity:

        

    Using Intent flags

    FLAG_ACTIVITY_NEW_TASK  同singleTask

    FLAG_ACTIVITY_SINGLE_TOP 同singleTop

    FLAG_ACTIVITY_CLEAR_TOP 如果之前启动过改activity,并且在当前任务栈中,启动activity时,会清空当前任务栈中该activity上面的所有activity,并触发onNewIntent(). 常和FLAG_ACTIVITY_NEW_TASK一起使用。

    使用intent的flags会覆盖mainfest中activity的启动模式。

    Handling affinities

    affinity表示某个activity属于哪个Task。默认情况下,一个应用的affinity值都相同,所以都属于同一个任务栈。不同的应用有不同的 affinity,同一个应用的不同activity也可能有不同的 affinity,做法如下:

    You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.

    taskAffinity 在两种情况下起作用:

    1.启动模式为singleTask,一般是新启动一个任务栈,但是如果存在与启动的activity taskAffinity相同的任务栈,就将activity放到那个任务栈中。

      注意,以singgleTask启动别的应用的activity后,如果此时用户按Home键回到主界面,点击应用的icon,只能回到我们自己的应用,启动别的应用的activity的任务栈是处于后台的。你可能需要做一些处理。

    2.设置了allowTaskReparenting 为 "true".

      举个例子:在我们的应用中启动了一个天气预报的界面,这个界面设置了allowTaskReparenting 为true,此时天气预报的界面会放到当前任务栈中,

      home键回到主界面,启动天气预报的应用,此时显示的是你在原来应用中启动的界面。

        Task A 有Activity A, 从A 启动 Application B 的 Activity B,Task A 为 A-B.

        Home键返回的主界面,启动Application B,系统创建一个新的Task B,(假设B的MainActivity为MB),所以Task B为MB-B。

    Clearing the back stack

    alwaysRetainTaskState

    If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.

    clearTaskOnLaunch

    If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it. In other words, it's the opposite ofalwaysRetainTaskState. The user always returns to the task in its initial state, even after a leaving the task for only a moment.

    finishOnTaskLaunch

    This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task. It can also cause any activity to go away, including the root activity. When it's set to "true", the activity remains part of the task only for the current session. If the user leaves and then returns to the task, it is no longer present.

    Starting a task

    <activity ... >
        <intent-filter ... >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        ...
    </activity>
    

    EXAMPLE

    一个应用中有两个Activity,SubProcessA,SubProcessB,都是singleTask,用A启动B后,任务栈为:A-B,再用B启动A,任务栈为A。

  • 相关阅读:
    RMAN动态视图
    无归档模式下的备份
    验证备份集-使用DBVERIFY工具
    手工备份控制文件和参数文件
    针对发起alter tablespace test begin backup 断电情况的处理
    Jenkins一次任务构建中如何处理多个git仓库
    Element-ui Tree组件实现单选
    前端覆盖式发布引发的使用体验提升
    客户端localStorage命名冲突问题
    git 查看和删除分支
  • 原文地址:https://www.cnblogs.com/aprz512/p/5106584.html
Copyright © 2011-2022 走看看