1.一个应用程序一般都是由多个activity组成的。

2.任务栈(task stack)(别名backstack后退栈)记录存放用户开启的activity的。

3.一个应用程序一被开启系统就给他分配一个任务栈,当所有的activity都退出的时候,任务栈就清空了。

4.任务栈的id是一个integer的数据类型 自增长的。

5.在android操作系统里面会存在多个任务栈,一个应用程序一个任务栈。

6.桌面应用和一般的应用程序是一样的,任务栈的行为也是一样的。

7.默认情况下,关闭掉一个应用程序,清空了这个应用程序的任务栈。应用程序的进程还会保留。

为什么要引入任务栈的概念:

window下,可以通过点击任务栏  切换任务

android下,长按小房子,切换任务。

为了记录用户开启了哪些activity,记录这些activity开启的先后顺序,google引入任务栈。(task stack)概念,帮助维护好的用户体验。

Activity的启动模式

Standard 默认标准的启动模式,每次startActivity都是创建一个新的activity的实例。适用于绝大多数情况。

singleTop 单一顶部,如果要开启的activity在任务栈的顶部已经存在,就不会创建新的实例。而是调用onNewIntent()方法。应用场景:浏览器书签。避免栈顶的activity被重复的创建,解决用户体验的问题。

singletask 单一任务栈 , activity只会在任务栈里面存在一个实例。如果要激活的activity,在任务栈里面已经存在,就不会创建新的activity,而是复用这个已经存在的activity,调用 onNewIntent() 方法,并且清空当前activity任务栈上面所有的activity

应用场景:浏览器activity, 整个任务栈只有一个实例,节约内存和cpu的目的

注意: activity还是运行在当前应用程序的任务栈里面的。不会创建新的任务栈。

singleInstance 单状态   单例模式

单一实例,整个手机操作系统里面只有一个实例存在,不同的应用去打开这个activity共享,公用的同一个activity。

它会运行在自己的单独,独立的任务栈里面,并且任务栈里面只有他一个实例存在。

应用场景:呼叫来点界面InCallScreen

案例,编写如下案例

1 android清单文件的内容如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.itheima.taskstack"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="19" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.itheima.taskstack.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name="com.itheima.taskstack.SecondActivity"

android:launchMode="singleInstance"

>

<intent-filter>

<action android:name="com.itheima.task.single"/>

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</activity>

</application>

</manifest>

2 布局文件activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="我是界面01"

android:textSize="30sp"/>

<Button

android:onClick="open01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="开启界面01"/>

<Button

android:onClick="open02"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="开启界面02"/>

</LinearLayout>

3 布局文件activity_second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="我是界面02"

android:textSize="30sp" />

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="open01"

android:text="开启界面01" />

<Button

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="open02"

android:text="开启界面02" />

</LinearLayout>

4 MainActivity

package com.itheima.taskstack;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

System.out.println("01activity被创建了。任务栈id:"+getTaskId());

}

public void open01(View view){

Intent intent = new Intent(this,MainActivity.class);

startActivity(intent);

}

public void open02(View view){

Intent intent = new Intent(this,SecondActivity.class);

startActivity(intent);

}

}

5 SecondActivity

package com.itheima.taskstack;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

public class SecondActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

System.out.println("02activity被创建了。任务栈id:" + getTaskId());

}

public void open01(View view) {

Intent intent = new Intent(this,MainActivity.class);

startActivity(intent);

}

public void open02(View view) {

Intent intent = new Intent(this,SecondActivity.class);

startActivity(intent);

}

@Override

protected void onNewIntent(Intent intent) {

System.out.println("o2activityonnew intent.任务栈id:" + getTaskId());

super.onNewIntent(intent);

}

}

15_Android中任务栈的更多相关文章

  1. 关于VS中更改栈和堆空间的大小

    编号:1008时间:2016年4月12日17:01:38功能:关于VS中更改栈和堆空间的大小 URL:http://blog.csdn.net/icerock2000/article/details/ ...

  2. IL中的栈和闪电的Owin推荐

    最近几天有幸得到闪电大哥的指点,了解了EMIT和IL中的一些指令.虽然有高射炮打蚊子的说法,但是我相信“二八定律”,80%的功能可以用20%的技术解决,20%的功能只能用80%的技术解决.大哥的博客: ...

  3. Lua 和 C 交互中虚拟栈的操作

    Lua 和 C 交互中虚拟栈的操作 /* int lua_pcall(lua_State *L, int nargs, int nresults, int msgh) * 以保护模式调用具有" ...

  4. Lua的函数调用和协程中,栈的变化情况

    Lua的函数调用和协程中,栈的变化情况 1. lua_call / lua_pcall   对于这两个函数,对栈底是没有影响的--调用的时候,参数会被从栈中移除,当函数返 回的时候,其返回值会从函数处 ...

  5. js 中的栈和堆

    js中的栈与堆的讲解/基本数据类型与引用类型的讲解 前言:1. 学习前端,入门简单,想学好确实是一件很困难的事情,东西多而且杂,版本快速迭代,产品框架层出不穷. 2. 前端学习成本确实很高,需要不断的 ...

  6. javascript中的栈、队列。

                           javascript中的栈.队列 栈方法     栈是一种LIFO(后进先出)的数据结构,在js中实现只需用到2个函数 push()  接受参数并将其放置 ...

  7. 面试01:解释内存中的栈(stack)、堆(heap)和方法区(method area)的用法

    栈的使用:通常我们定义一个基本数据类型的变量,一个对象的引用,还有就是函数调用的现场保存都使用JVM中的栈空间. 队的使用:通过new关键字和构造器创建的对象则放在堆空间,堆是垃圾收集器管理的主要区域 ...

  8. 浅谈Java中的栈和堆

    人们常说堆栈堆栈,堆和栈是内存中两处不一样的地方,什么样的数据存在栈,又是什么样的数据存在堆中? 这里浅谈Java中的栈和堆 首先,将结论写在前面,后面再用例子加以验证. Java的栈中存储以下类型数 ...

  9. Java中 堆 栈,常量池等概念解析(转载)

    1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(字符 ...

随机推荐

  1. ctf writeup之程序员密码

    起因 在v2ex上看到有人发了一篇帖子,说做了一个程序员小游戏,遂试玩了一下. 游戏的地址在这里: http://www.bettertomissthantomeet.com/pages/level. ...

  2. Docker内核能力机制

    能力机制(Capability)是 Linux 内核一个强大的特性,可以提供细粒度的权限访问控制. Linux 内核自 2.2 版本起就支持能力机制,它将权限划分为更加细粒度的操作能力,既可以作用在进 ...

  3. 前端技术之_CSS详解第一天

    前端技术之_CSS详解第一天 一html部分 略.... 二.列表 列表有3种 2.1 无序列表 无序列表,用来表示一个列表的语义,并且每个项目和每个项目之间,是不分先后的. ul就是英语unorde ...

  4. MacOS下对postgresql的简单管理操作

    如何安装在另一篇blog中有述,这里不再赘述.本篇简单说一下安装完postgresql之后的一些管理和查询操作. 首先安装完postgresql之后需要初始化数据库: initdb /usr/loca ...

  5. Excel 数据验证宏

    Sub 宏1() ' ' 宏1 宏 ' ' With Selection.Validation .Delete .Add Type:=xlValidateList, AlertStyle:=xlVal ...

  6. Xcode的playground中对于SpriteKit物理对象的更新为何无效

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了便于SpriteKit中物理行为的调试,我们可以借助于Xc ...

  7. 开源控件ViewPagerIndicator的使用

    此文转载自http://www.jianshu.com/p/a2263ee3e7c3 前几天学习了ViewPager作为引导页和Tab的使用方法.后来也有根据不同的使用情况改用Fragment作为Ta ...

  8. Android JavascriptBridge 详解(二)

    原文出自:http://blog.csdn.net/sk719887916/article/details/47189607 Android开发目前现状来说,开发者大部分时间花在UI的屏幕适配上,使用 ...

  9. UE4 C++与蓝图交互

    一.C++调用蓝图的函数 本来应该有一个很好的示例,但一下却忘了适用于哪些地方,只有简单写一个步骤了. 1.新建一个C++类继承Actor,名称随意,我这里继承了一个SplineMesh,一样de,并 ...

  10. Oracle中使用游标转换数据表中指定字段内容格式(拼音转数字)

    应用场景:将数据表TB_USER中字段NNDP的内容中为[sannanyinv]转换为[3男1女] 主要脚本:一个游标脚本+分割字符串函数+拼音转数字脚本 操作步骤如下: 1.创建类型 create ...