当前位置: 移动技术网 > IT编程>移动开发>Android > Android activity堆栈及管理实例详解

Android activity堆栈及管理实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

庄巧涵第二季,天佑阿哲,另类喊麦

本示例演示如何通过设置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堆栈及管理实例详解,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网