引用:http://blog.csdn.net/think_soft/article/details/7477072

本示例演示如何通过设置Intent对象的标记,来改变当前任务堆栈中既存的Activity的顺序。

1. Intent对象的Activity启动标记说明:

FLAG_ACTIVITY_BROUGHT_TO_FRONT:

应用程序代码中通常不设置这个标记,而是由系统给单任务启动模式的Activity的设置。

FLAG_ACTIVITY_CLEAR_TASK:

如果给Intent对象添加了这个标记,那么在Activity被启动之前,会导致跟这个Activity关联的任何既存的任务都被清除。也就是说新的Activity会成为一个空任务的根,而其他任何Activity都会被销毁。它紧跟FLAG_ACTIVITY_NEW_TASK联合使用。

FLAG_ACTIVITY_CLEAR_TOP:

如果给Intent对象设置这个标记,并且要启动的Activity在当前任务中已经运行了,那么不是创建一个这个Activity的新的实例,而是把堆栈中这个Activity之上的所有其他Activity都关掉,然后把新的Intent对象发送给这个既存的Activity(这时它在堆栈的顶部)。

FLAG_ACTIVITY_CLEAR_WHEN_TASK_REST:

如果给Intent对象设置了这个标记,那么在这个任务被复位时,在任务的Activity堆栈中这个标记点之后的Activity都应该被清除。

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:

如果给Intent对象设置了这个标记,那么新的Activity不会被保留在最近启动的Activity的列表中。

FLAG_ACTIVITY_FORWARD_RESULT:

如果给Intent对象设置了这个标记,并且这个Intent对象被用于从一个既存的Activity中启动一个新的Activity,然后将这个既存Activity的回复目标转移到新的Activity。使用这种方式获取的新的Activity能够调用setResult(int)方法,把结果返回给原始的Activity。

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY:

这个标记通常不由应用程序代码来设置,如果是从历史中启动这个Activity,系统就会设置这个标记。

FLAG_ACTIVITY_MULTIPLE_TASK:

除非实现自己的顶层应用程序启动器,否则不使用这个标记。

FLAG_ACTIVITY_NEW_TASK:

如果给Intent对象设置了这个标记,在历史堆栈之上,这个Activity将成为一个新任务的起点。

FLAG_ACTIVITY_NO_ANIMATION:

如果给Intent对象设置了这个标记,那么将会阻止系统在Activity间切换的动画变换。

FALG_ACTIVITY_NO_HISTORY:

如果给Intent对象设置了这个标记,那么新的Activity将不会被保留在历史堆栈中。

FLAG_ACTIVITY_NO_USER_ACTION:

如果给Intent对象设置了这个标记,在新启动到前台的Activity被挂起之前,它会阻止

普通的onUserLeaveHint()方法的回调。如果电话拨号或闹钟程序就要使用这个标记来启动Activity。

FLAG_ACTIVITY_PREVIOUS_IS_TOP:

如果给Intent对象设置了这个标记,并且这个Intent对象被用于从一个既存的Activity中启动一个新的Activity,这个Activity不能用于接受发送给顶层Activity的新的Intent对象,通常认为使用这个标记启动的Activity会被自己立即终止。

FLAG_ACTIVITY_REORDER_TO_FRONT:

如果给Intent对象设置了这个标记,那么将会导致任务历史堆栈中既存的Activity被带到前台。

FLAG_ACTIVITY_RESET_TASK_IF_NEEDED:

如果给Intent对象设置了这个标记,并且这个Activity在一个新任务中被启动,也可以在既存的任务堆栈中被带到顶层,那么它就会被作为任务的前门来启动。

FLAG_ACTIVITY_SINGLE_TOP:

如果给Intent对象设置了这个标记,如果要启动的Activity已经在历史堆栈的顶层运行,那么这个Activity就不会被启动。

FLAG_ACTIVITY_TASK_ON_HOME:

如果给Intent对象设置了这个标记,那么它会导致新启动的任务被放到当前的主Activity任务之上。

2. 示例代码

2.1. 定义清单文件(AndroidManifest.xml)

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

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

package="my.android.test"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<activity android:name=".ReorderOnLaunch"

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=".ReorderTwo" />

<activity android:name=".ReorderThree" />

<activity android:name=".ReorderFour" />

</application>

<uses-sdk android:minSdkVersion="9" />\

</manifest>

2.2. 定义字符串资源(strings.xml)

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

<resources>

<string name="hello">Hello World, ReorderOnLaunch!</string>

<string name="app_name">ReorderOnLaunch</string>

<string name="reorder_on_launch">This is the first of a sequence of four Activities.  A button on the fourth will use the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT flag to bring the second of the activities to the front of the history stack. After that, proceeding back through the history should begin with the newly-frontmost second reorder activity, then the fourth, the third, and finally the first.</string>

<string name="reorder_launch_two">Go to the second</string>

<string name="reorder_two_text">This is the second in a sequence of four Activities.</string>

<string name="reorder_launch_three">Go to the third</string>

<string name="reorder_three_text">This is the third of a sequence of four Activities.</string>

<string name="reorder_launch_four">Go to the fourth</string>

<string name="reorder_four_text">This is the last in a sequence of four Activities.</string>

<string name="reorder_second_to_front">Bring the second in front</string>

</resources>

2.3. 定义布局文件

reorder_on_launch.xml

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

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

android:orientation="vertical"

android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/reorder_on_launch"/>

<Button android:id="@+id/reorder_launch_two"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/reorder_launch_two">

</Button>

</LinearLayout>

reorder_two.xml

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

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

android:orientation="vertical"

android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/reorder_two_text"/>

<Button android:id="@+id/reorder_launch_three"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/reorder_launch_three">

</Button>

</LinearLayout>

reorder_three.xml

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

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

android:orientation="vertical"

android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/reorder_three_text"/>

<Button android:id="@+id/reorder_launch_four"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/reorder_launch_four">

</Button>

</LinearLayout>

reorder_four.xml

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

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

android:orientation="vertical"

android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/reorder_four_text"/>

<Button android:id="@+id/reorder_second_to_front"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/reorder_second_to_front">

</Button>

</LinearLayout>

2.4. 创建Activity

ReorderOnLaunch.java

package my.android.test;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass ReorderOnLaunch extends Activity {

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.reorder_on_launch);

Button twoButton = (Button)findViewById(R.id.reorder_launch_two);

twoButton.setOnClickListener(mClickListener);

}

privatefinal OnClickListener mClickListener = new OnClickListener(){

publicvoid onClick(View v){

startActivity(new Intent(ReorderOnLaunch.this, ReorderTwo.class));

}

};

}

ReorderTwo.java

package my.android.test;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass ReorderTwo extends Activity {

@Override

protectedvoid onCreate(Bundle saveState){

super.onCreate(saveState);

setContentView(R.layout.reorder_two);

Button twoButton = (Button)findViewById(R.id.reorder_launch_three);

twoButton.setOnClickListener(mClickListener);

}

privatefinal OnClickListener mClickListener = new OnClickListener(){

publicvoid onClick(View v){

startActivity(new Intent(ReorderTwo.this, ReorderThree.class));

}

};

}

ReorderThree.java

package my.android.test;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass ReorderThree extends Activity {

privatefinal OnClickListener mClickListener = new OnClickListener(){

publicvoid onClick(View v){

startActivity(new Intent(ReorderThree.this, ReorderFour.class));

}

};

@Override

protectedvoid onCreate(Bundle saveState){

super.onCreate(saveState);

setContentView(R.layout.reorder_three);

Button twoButton = (Button)findViewById(R.id.reorder_launch_four);

twoButton.setOnClickListener(mClickListener);

}

}

ReorderFour.java

package my.android.test;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass ReorderFour extends Activity {

@Override

protectedvoid onCreate(Bundle saveState){

super.onCreate(saveState);

setContentView(R.layout.reorder_four);

Button twoButton = (Button)findViewById(R.id.reorder_second_to_front);

twoButton.setOnClickListener(mClickListener);

}

privatefinal OnClickListener mClickListener = new OnClickListener(){

publicvoid onClick(View v){

Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);

           intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivity(intent);

}

};

}

如何改变Activity在当前任务堆栈中的顺序,Intent参数大全的更多相关文章

  1. C++中结构体与类的区别(结构不能被继承,默认是public,在堆栈中创建,是值类型,而类是引用类型)good

    结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型. 1.结构的构造函数和类的构造函数不同. a. ...

  2. 直接修改托管堆栈中的type object pointer(类型对象指针)

    都知道.NET是一个强对象类型的框架. 那么对于对象类型又是怎么确定的呢. 最初的我简单认为数据的类型就是定义时字段的类型修饰决定的(回来发现这种观点是绝对错误的) 我们知道引用对象存储在托管堆栈中, ...

  3. poj 1363 Rails in PopPush City &&【求堆栈中合法出栈顺序次数】

    问题如下: 问题 B: Rails 时间限制: Sec 内存限制: MB 提交: 解决: [提交][状态][讨论版] 题目描述 There is a famous railway station in ...

  4. Android 儿子Activity在启动过程中的流程组件 &amp;&amp; 儿子Activity在一个新的进程组件启动过程

    1.儿子Activity在启动过程中的流程组件 在Android Activity启动过程http://blog.csdn.net/jltxgcy/article/details/35984557一文 ...

  5. Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?

    在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...

  6. 在堆栈中,push为入栈操作,pop为出栈操作

    LinkedList提供以下方法:(ArrayList无此类方法) addFirst(); removeFirst(); addLast(); removeLast(); 在堆栈中,push为入栈操作 ...

  7. MVC中使用分部视图参数,改变分部视图连接样式

    MVC中使用分部视图参数,改变分部视图连接样式! Controller代码 [ChildActionOnly] public ActionResult Navigator(int tag) { ret ...

  8. addBack() 添加堆栈中元素集合到当前集合,一个选择性的过滤选择器。

    addBack() 概述 添加堆栈中元素集合到当前集合,一个选择性的过滤选择器. 如上所述在讨论中的.end(), jQuery对象维护一个堆栈内部来跟踪匹配的元素集合的变化.当一个DOM遍历方法被调 ...

  9. Android四大组件之Activity一(组件的概念、Intent、监听)

    前言知识补充:  什么是组件?   1.它的类必须实现特定接口或继承特定类   2.需要在配置文件中配置其全类名   3.它的对象不是通过new来创建的, 而是系统自动创建的   4.它的对象具有一定 ...

随机推荐

  1. ASP.NET MVC请求处理管道生命周期的19个关键环节(13-19)

    在上一篇"ASP.NET MVC请求处理管道生命周期的19个关键环节(7-12) ",体验了7-12关键环节,本篇继续. ⒀当请求到达UrlRoutingModule的时候,Url ...

  2. 使用VS2013在WIN8.1上运行gaclib的hello world

    首先:gaclib的官网是http://www.gaclib.net/ 需要了解更多信息的请自己去官网,我也是刚刚研究   第一步 下载gaclib的源码   这些文件是运行程序所必须的   第二步 ...

  3. [安卓] 8、VIEW和SURFACEVIEW游戏框架

    这是个简单的游戏框架,上图显示我们实现了屏幕上对象的位置控制.这里要1个简单的layout资源和2个java类:在MainActivity中主要和以往一样,唯一不同的是去除电池图标和标题等操作,然后第 ...

  4. jpa 注解使用说明

    1.@Entity(name="EntityName") 必须,name为可选,对应数据库中一的个表 2.@Table(name="",catalog=&quo ...

  5. HTML5 canvas globalCompositeOperation绘图类型讲解

    我们总是将一个图形画在另一个之上,大多数情况下,这样是不够的.比如说,它这样受制于图形的绘制顺序.不过,我们可以利用 globalCompositeOperation 属性来改变这些做法.global ...

  6. 构建单页Web应用

    摘自前端农民工的博客 让我们先来看几个网站: coding teambition cloud9 注意这几个网站的相同点,那就是在浏览器中,做了原先“应当”在客户端做的事情.它们的界面切换非常流畅,响应 ...

  7. 《精通移动app测试实战:技术、工具和案例》新书上市

    本书是测试专家.性能测试专家.专业畅销书作者--于涌,多年实战经验的总结,涵盖主流的测试工具,包括众多的测试实例,涵盖单元测试.功能测试.性能测试.UI测试.手游测试.自动化测试.测试用例管理.持续集 ...

  8. 5分钟破解wpa2密码(转)

    首先大家要明白一种数学运算,它叫做哈希算法(hash),这是一种不可逆运算,你不能通过运算结果来求解出原来的未知数是多少,有时我们还需要不同的未知数通过该算法计算后得到的结果不能相同,即你不太可能找到 ...

  9. 专访高磊:安卓APK安全加固的引领者

    高磊,爱加密CEO,安卓巴士版主之一,曾编写河南省某地市交通信息化规划十二五规划,以及参与省厅级资源共享平台设计等.之前的工作经理积累了丰富的设计规划经验,此外还具有J2EE和 Android开发经验 ...

  10. mysql5.5 uuid做主键与int做主键的性能实测

    数据库:mysql5.5 表类型:InnoDB 数据量:100W条 第一种情况: 主键采用uuid 32位. 运行查询语句1:SELECT COUNT(id) FROM test_varchar; 运 ...