日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

15_Android中任务栈

發布時間:2024/9/27 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 15_Android中任务栈 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


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中任务栈的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。