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);

         }

}

 

原文地址:https://www.cnblogs.com/wzjhoutai/p/6856884.html