2.安卓基礎之Activity啓動方式

零、前言

standard:默認的啓動模式
    |---啓動一個Activity,該Activity就會在返回棧中入棧
    |---該Activity處於棧頂的位置
    |---每次啓動都會建立該Activity的新實例
    
singleTop:
    |---在啓動活動時若棧頂已是該Activity,則認爲能夠直接使用它
    
singleTask:
    |---整個應用程序的上下文中只存在一個實例
    |---兩次相同實例之間的Activity會被殺死

singleInstance:
    |---啓用一個新的返回棧來管理這個活動

1、測試類

Activity1
public class Activity1 extends AppCompatActivity {

    @BindView(R.id.btn_start_self)
    Button mBtnStartSelf;
    @BindView(R.id.button_start_others)
    Button mButtonStartOthers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_task);
        ButterKnife.bind(this);
        Log.e("TASK_ID", "Activity1 Task id is " + getTaskId());
    }

    @OnClick({R.id.btn_start_self, R.id.button_start_others})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start_self:
                startActivity(new Intent(Activity1.this, Activity1.class));
                break;
            case R.id.button_start_others:
                startActivity(new Intent(Activity1.this, Activity2.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("TASK_ID", "Activity1 銷燬");
    }
}
Activity2
public class Activity2 extends AppCompatActivity {

    @BindView(R.id.btn_start_self)
    Button mBtnStartSelf;
    @BindView(R.id.button_start_others)
    Button mButtonStartOthers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_task);
        ButterKnife.bind(this);

        Log.e("TASK_ID", "Activity2 Task id is " + getTaskId());
    }

    @OnClick({R.id.btn_start_self, R.id.button_start_others})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start_self:
                startActivity(new Intent(Activity2.this, Activity2.class));
                break;
            case R.id.button_start_others:
                startActivity(new Intent(Activity2.this, Activity1.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("TASK_ID", "Activity2 銷燬");
    }
}
佈局:ac_task.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_start_self"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="打開本身"
        android:textAllCaps="false"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="30dp"/>

    <Button
        android:id="@+id/button_start_others"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="打開別人"
        app:layout_constraintStart_toEndOf="@+id/btn_start_self"
        app:layout_constraintTop_toTopOf="parent"/>
    
</android.support.constraint.ConstraintLayout>

3、測試結果分析:

1:standard模式:Activity一、2皆爲standard

standard.gif

依次打開Activity一、二、二、一、2
 E/TASK_ID: Activity1 Task id is 89
 E/TASK_ID: Activity2 Task id is 89
 E/TASK_ID: Activity2 Task id is 89
 E/TASK_ID: Activity1 Task id is 89
 E/TASK_ID: Activity2 Task id is 89
依次返回
 E/TASK_ID: Activity2 銷燬
 E/TASK_ID: Activity1 銷燬
 E/TASK_ID: Activity2 銷燬
 E/TASK_ID: Activity2 銷燬
 E/TASK_ID: Activity1 銷燬

standard .png

:2:singleTop模式:Activity1爲standard, Activity2 爲singleTop

singleTop.gif

依次打開Activity一、二、二、一、2
E/TASK_ID: Activity1 Task id is 82
E/TASK_ID: Activity2 Task id is 82
E/TASK_ID: Activity1 Task id is 82
E/TASK_ID: Activity1 Task id is 82
依次返回
E/TASK_ID: Activity1 銷燬
E/TASK_ID: Activity1 銷燬
E/TASK_ID: Activity2 銷燬
E/TASK_ID: Activity1 銷燬
可見第二次打開Activity2時並沒有變化,由於singleTop模式,第二次的Activity2並無建立

singleTop.png


:3:singleTask模式:Activity1爲standard, Activity2 爲singleTask

singleTask.gif

依次打開Activity一、二、二、一、2
E/TASK_ID: Activity1 Task id is 94
E/TASK_ID: Activity2 Task id is 94
E/TASK_ID: Activity1 Task id is 94
E/TASK_ID: Activity1 銷燬
依次返回
E/TASK_ID: Activity2 銷燬
E/TASK_ID: Activity1 銷燬
可見第二次打開Activity2時並沒有變化,第三次打開Activity2,Activity1銷燬

singleTask.png

:4:singleInstance模式:Activity1爲standard, Activity2 singleInstance

singleInstance.gif

依次打開Activity一、二、二、一、2
 E/TASK_ID: Activity1 Task id is 115
 E/TASK_ID: Activity2 Task id is 116
 E/TASK_ID: Activity1 Task id is 115
依次返回
 E/TASK_ID: Activity2 銷燬
 E/TASK_ID: Activity1 銷燬
 E/TASK_ID: Activity1 銷燬
可見Activity2單獨在一個棧中,屢次開啓Activity2不會新建實例

singleInstance.png


後記、

1.聲明:
1.本文由張風捷特烈原創,轉載請註明
2.歡迎廣大編程愛好者共同交流
3.我的能力有限,若有不正之處歡迎你們批評指證,一定虛心改正
4.看到這裏,感謝你的喜歡與支持
2.鏈接傳送門:

更多安卓技術歡迎訪問:安卓技術棧
個人github地址:歡迎star
張風捷特烈我的網站:http://www.toly1994.comandroid

3.聯繫我
QQ:1981462002 郵箱:1981462002@qq.com 微信:zdl1994328
相關文章
相關標籤/搜索