Android activity堆栈及管理实例详解
作者:x1299906945 发布时间:2022-07-28 01:17:14
本示例演示如何通过设置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" />
</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" />
</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" />
</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" />
</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;
public class ReorderOnLaunch extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reorder_on_launch);
Button twoButton = (Button)findViewById(R.id.reorder_launch_two);
twoButton.setOnClickListener(mClickListener);
}
private final OnClickListener mClickListener = new OnClickListener(){
public void 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;
public class ReorderTwo extends Activity {
@Override
protected void onCreate(Bundle saveState){
super.onCreate(saveState);
setContentView(R.layout.reorder_two);
Button twoButton = (Button)findViewById(R.id.reorder_launch_three);
twoButton.setOnClickListener(mClickListener);
}
private final 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;
public class ReorderThree extends Activity {
private final OnClickListener mClickListener = new OnClickListener(){
public void onClick(View v){
startActivity(new Intent(ReorderThree.this, ReorderFour.class));
}
};
@Override
protected void 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
protected void onCreate(Bundle saveState){
super.onCreate(saveState);
setContentView(R.layout.reorder_four);
Button twoButton = (Button)findViewById(R.id.reorder_second_to_front);
twoButton.setOnClickListener(mClickListener);
}
private final OnClickListener mClickListener = new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
};
}
3.activity堆栈管理类
package net.oschina.app;
import java.util.Stack;
import android.app.Activity;
import android.content.Context;
/**
* activity堆栈式管理
*/
public class AppManager {
private static Stack<Activity> activityStack;
private static AppManager instance;
private AppManager() {}
/**
* 单一实例
*/
public static AppManager getAppManager() {
if (instance == null) {
instance = new AppManager();
}
return instance;
}
/**
* 添加Activity到堆栈
*/
public void addActivity(Activity activity) {
if (activityStack == null) {
activityStack = new Stack<Activity>();
}
activityStack.add(activity);
}
/**
* 获取当前Activity(堆栈中最后一个压入的)
*/
public Activity currentActivity() {
Activity activity = activityStack.lastElement();
return activity;
}
/**
* 结束当前Activity(堆栈中最后一个压入的)
*/
public void finishActivity() {
Activity activity = activityStack.lastElement();
finishActivity(activity);
}
/**
* 结束指定的Activity
*/
public void finishActivity(Activity activity) {
if (activity != null && !activity.isFinishing()) {
activityStack.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 结束指定类名的Activity
*/
public void finishActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
break;
}
}
}
/**
* 结束所有Activity
*/
public void finishAllActivity() {
for (int i = 0, size = activityStack.size(); i < size; i++) {
if (null != activityStack.get(i)) {
finishActivity(activityStack.get(i));
break;
}
}
activityStack.clear();
}
/**
* 获取指定的Activity
*/
public static Activity getActivity(Class<?> cls) {
if (activityStack != null)
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
return activity;
}
}
return null;
}
/**
* 退出应用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
// 杀死该应用进程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
} catch (Exception e) {
}
}
}
以上所述是小编给大家介绍的Android activity堆栈及管理实例详解网站的支持!
来源:http://blog.csdn.net/x1299906945/article/details/52467857


猜你喜欢
- 生成文字图片:/// <summary> /// 生成文字图片 /// &l
- 本文实例讲述了Android实现从网络获取图片显示并保存到SD卡的方法。分享给大家供大家参考,具体如下:问题:如何不断获取图片并显示出来,达
- 效果图: //偶数随机 Random evenRanm
- 一 .概述先讲缓存实现,主要是mybatis一级缓存,二级缓存及缓存使用后续补充Mybatis缓存的实现是基于Map的,从缓存里面读写数据是
- 本文实例为大家分享了java转树形结构工具类的具体代码,供大家参考,具体内容如下import com.alibaba.fastjson.JS
- 本文实例讲述了C#实现抓取和分析网页类。分享给大家供大家参考。具体分析如下:这里介绍了抓取和分析网页的类。其主要功能有:1、提取网页的纯文本
- Map集合的概述概述:interface Map<K,V> 其中K是键的类型,键是唯一的,不重复。V是值的类型,是可以重复。且每
- 1.for循环import com.google.common.base.Function;import com.google.common
- 前言解释:之前用的ScreenToGif录屏,因为上传的.gif最大不超过5MB,所以做了不少删帧和色彩弱化等处理,这才导致色彩看上去不是很
- 背景:由于所在办公室网络限制,笔者每天都使用网络都要先连接无线网。如下图,输入授权用户信息登录后才能使用WIFI。丧心病狂的是该网页Cook
- 简介功能需求如图所示,点击下一个按钮,所有卡片向右滚动,其中最后一张需要变更为最前面的一张,点击上一个按钮,所有卡片向左滚动,最前面的一张需
- 1.Java运行环境搭建,对于初学者来说,主要下载安装jdk即可,windows操作系统再配合记事本,即可进行java程序开发。后续的学习以
- 一、读取系统配置文件application.yaml1、application.yaml配置文件中增加一下测试配置testdata: &nb
- 问题描述:N个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续从1开始报数,报到m的人出圈;如此往复,直到所有人出圈很多实现是使
- java 删除链表中的元素以下实例演示了使用 Clear() 方法来删除链表中的元素:import java.util.*;public c
- 1. ObsoleteAttributeObsoleteAttribute 适用于除组件、模块、参数和返回值以外的所有程序元素。将元素标记为
- 通过AIDL接口在进程间传递数据,记录在开发中遇到的一写问题AIDL支持数据类型如下:1. Java 的原生类型2. String 和Cha
- Java%(取模运算)Java的取模运算1.实现算法public static double ramainder(double divide
- 文件上传页面<%@ page language="java" contentType="text/htm
- JPA连接到数据库,调用存储过程,这样的需求很常见。本文就针对这一点,讲述如何使用spring Data JPA调用存储过程的方法。1、存储