最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android activity堆棧及管理實例詳解

 更新時間:2016年09月08日 10:29:58   作者:x1299906945  
這篇文章主要介紹了Android activity堆棧及管理實例詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,對android activity堆棧相關(guān)知識感興趣的朋友一起學習吧

本示例演示如何通過設(shè)置Intent對象的標記,來改變當前任務(wù)堆棧中既存的Activity的順序。

1. Intent對象的Activity啟動標記說明:

FLAG_ACTIVITY_BROUGHT_TO_FRONT

應(yīng)用程序代碼中通常不設(shè)置這個標記,而是由系統(tǒng)給單任務(wù)啟動模式的Activity的設(shè)置。

FLAG_ACTIVITY_CLEAR_TASK

如果給Intent對象添加了這個標記,那么在Activity被啟動之前,會導致跟這個Activity關(guān)聯(lián)的任何既存的任務(wù)都被清除。也就是說新的Activity會成為一個空任務(wù)的根,而其他任何Activity都會被銷毀。它緊跟FLAG_ACTIVITY_NEW_TASK聯(lián)合使用。

FLAG_ACTIVITY_CLEAR_TOP

如果給Intent對象設(shè)置這個標記,并且要啟動的Activity在當前任務(wù)中已經(jīng)運行了,那么不是創(chuàng)建一個這個Activity的新的實例,而是把堆棧中這個Activity之上的所有其他Activity都關(guān)掉,然后把新的Intent對象發(fā)送給這個既存的Activity(這時它在堆棧的頂部)。

FLAG_ACTIVITY_CLEAR_WHEN_TASK_REST

如果給Intent對象設(shè)置了這個標記,那么在這個任務(wù)被復位時,在任務(wù)的Activity堆棧中這個標記點之后的Activity都應(yīng)該被清除。

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

如果給Intent對象設(shè)置了這個標記,那么新的Activity不會被保留在最近啟動的Activity的列表中。

FLAG_ACTIVITY_FORWARD_RESULT

如果給Intent對象設(shè)置了這個標記,并且這個Intent對象被用于從一個既存的Activity中啟動一個新的Activity,然后將這個既存Activity的回復目標轉(zhuǎn)移到新的Activity。使用這種方式獲取的新的Activity能夠調(diào)用setResult(int)方法,把結(jié)果返回給原始的Activity。

FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY

這個標記通常不由應(yīng)用程序代碼來設(shè)置,如果是從歷史中啟動這個Activity,系統(tǒng)就會設(shè)置這個標記。

FLAG_ACTIVITY_MULTIPLE_TASK

除非實現(xiàn)自己的頂層應(yīng)用程序啟動器,否則不使用這個標記。

FLAG_ACTIVITY_NEW_TASK

如果給Intent對象設(shè)置了這個標記,在歷史堆棧之上,這個Activity將成為一個新任務(wù)的起點。

FLAG_ACTIVITY_NO_ANIMATION

如果給Intent對象設(shè)置了這個標記,那么將會阻止系統(tǒng)在Activity間切換的動畫變換。

FALG_ACTIVITY_NO_HISTORY

如果給Intent對象設(shè)置了這個標記,那么新的Activity將不會被保留在歷史堆棧中。

FLAG_ACTIVITY_NO_USER_ACTION

如果給Intent對象設(shè)置了這個標記,在新啟動到前臺的Activity被掛起之前,它會阻止普通的onUserLeaveHint()方法的回調(diào)。如果電話撥號或鬧鐘程序就要使用這個標記來啟動Activity。

FLAG_ACTIVITY_PREVIOUS_IS_TOP

如果給Intent對象設(shè)置了這個標記,并且這個Intent對象被用于從一個既存的Activity中啟動一個新的Activity,這個Activity不能用于接受發(fā)送給頂層Activity的新的Intent對象,通常認為使用這個標記啟動的Activity會被自己立即終止。

FLAG_ACTIVITY_REORDER_TO_FRONT

如果給Intent對象設(shè)置了這個標記,那么將會導致任務(wù)歷史堆棧中既存的Activity被帶到前臺。

FLAG_ACTIVITY_RESET_TASK_IF_NEEDED

如果給Intent對象設(shè)置了這個標記,并且這個Activity在一個新任務(wù)中被啟動,也可以在既存的任務(wù)堆棧中被帶到頂層,那么它就會被作為任務(wù)的前門來啟動。

FLAG_ACTIVITY_SINGLE_TOP

如果給Intent對象設(shè)置了這個標記,如果要啟動的Activity已經(jīng)在歷史堆棧的頂層運行,那么這個Activity就不會被啟動。

FLAG_ACTIVITY_TASK_ON_HOME

如果給Intent對象設(shè)置了這個標記,那么它會導致新啟動的任務(wù)被放到當前的主Activity任務(wù)之上。

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. 創(chuàng)建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;
}
/**
* 結(jié)束當前Activity(堆棧中最后一個壓入的)
*/
public void finishActivity() {
Activity activity = activityStack.lastElement();
finishActivity(activity);
}
/**
* 結(jié)束指定的Activity
*/
public void finishActivity(Activity activity) {
if (activity != null && !activity.isFinishing()) {
activityStack.remove(activity);
activity.finish();
activity = null;
}
}
/**
* 結(jié)束指定類名的Activity
*/
public void finishActivity(Class<?> cls) {
for (Activity activity : activityStack) {
if (activity.getClass().equals(cls)) {
finishActivity(activity);
break;
}
}
}
/**
* 結(jié)束所有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;
}
/**
* 退出應(yīng)用程序
*/
public void AppExit(Context context) {
try {
finishAllActivity();
// 殺死該應(yīng)用進程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
} catch (Exception e) {
}
}
}

以上所述是小編給大家介紹的Android activity堆棧及管理實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android自定義view實現(xiàn)TextView方形輸入框

    Android自定義view實現(xiàn)TextView方形輸入框

    這篇文章主要為大家詳細介紹了Android自定義view實現(xiàn)TextView方形輸入框,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android協(xié)程的7個重要知識點匯總

    Android協(xié)程的7個重要知識點匯總

    在現(xiàn)代Android應(yīng)用開發(fā)中,協(xié)程(Coroutine)已經(jīng)成為一種不可或缺的技術(shù),它不僅簡化了異步編程,還提供了許多強大的工具和功能,可以在高階場景中發(fā)揮出色的表現(xiàn),本文將深入探討Coroutine重要知識點,幫助開發(fā)者更好地利用Coroutine來構(gòu)建高效的Android應(yīng)用
    2023-09-09
  • Android?OpenCV基礎(chǔ)API清晰度亮度識別檢測

    Android?OpenCV基礎(chǔ)API清晰度亮度識別檢測

    這篇文章主要為大家介紹了Android?OpenCV基礎(chǔ)API清晰度亮度識別檢測,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 使用CountDownTimer類輕松實現(xiàn)倒計時功能

    使用CountDownTimer類輕松實現(xiàn)倒計時功能

    Android中有個countDownTimer類,從名字上就可以看出來,它的功能是記錄下載時間,將后臺線程的創(chuàng)建和Handler隊列封裝成為了一個方便的調(diào)用。
    2014-07-07
  • Android實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能

    Android實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能

    大家在日常使用spp流量文章的時候經(jīng)常會遇到這樣的一個功能,點擊文章的圖片進入圖片的瀏覽模式,可以左右滑動圖片瀏覽,并且可以實現(xiàn)保存圖片的功能,所以本文主要就介紹了在Android如何實現(xiàn)點擊WebView界面中圖片滑動瀏覽與保存圖片功能,需要的朋友可以參考下。
    2017-04-04
  • Android實現(xiàn)網(wǎng)絡(luò)多線程斷點續(xù)傳下載實例

    Android實現(xiàn)網(wǎng)絡(luò)多線程斷點續(xù)傳下載實例

    本示例介紹在Android平臺下通過HTTP協(xié)議實現(xiàn)斷點續(xù)傳下載。具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • android Launcher AppWidget添加步驟介紹

    android Launcher AppWidget添加步驟介紹

    大家好,本篇文章主要講的是android Launcher AppWidget添加步驟介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例

    Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例

    本篇文章主要介紹了Android利用ViewDragHelper輕松實現(xiàn)拼圖游戲的示例,非常具有實用價值,需要的朋友可以參考下
    2017-11-11
  • Android版音樂播放器

    Android版音樂播放器

    這篇文章主要為大家詳細介紹了Android版音樂播放器的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android自定義View實現(xiàn)圓環(huán)交替效果

    Android自定義View實現(xiàn)圓環(huán)交替效果

    這篇文章給大家介紹如何基于Android自定義View實現(xiàn)圓環(huán)交替的效果,實現(xiàn)后效果很贊,有需要的小伙伴們可以參考借鑒。
    2016-08-08

最新評論

长武县| 应用必备| 万州区| 赣榆县| 尖扎县| 玉山县| 遂宁市| 华池县| 建德市| 寻乌县| 博兴县| 宣威市| 丹寨县| 赫章县| 海淀区| 凤阳县| 伊春市| 利辛县| 扎赉特旗| 大田县| 庆安县| 抚顺市| 安国市| 望都县| 台东市| 霍林郭勒市| 苏尼特右旗| 盱眙县| 吴忠市| 聂荣县| 莆田市| 出国| 肃北| 绥江县| 新安县| 西峡县| 晋中市| 宾阳县| 吉安市| 柯坪县| 新蔡县|