2-AI--Activity启动方式

作者: e4e52c116681 | 来源:发表于2018-08-23 17:10 被阅读0次

    零、前言

    standard:默认的启动模式
        |---启动一个Activity,该Activity就会在返回栈中入栈
        |---该Activity处于栈顶的位置
        |---每次启动都会创建该Activity的新实例
        
    singleTop:
        |---在启动活动时若栈顶已经是该Activity,则认为可以直接使用它
        
    singleTask:
        |---整个应用程序的上下文中只存在一个实例
        |---两次相同实例之间的Activity会被杀死
    
    singleInstance:
        |---启用一个新的返回栈来管理这个活动
    

    一、测试类

    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>
    

    三、测试结果分析:

    1:standard模式:Activity1、2皆为standard
    standard.gif
    依次打开Activity1、2、2、1、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
    依次打开Activity1、2、2、1、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
    依次打开Activity1、2、2、1、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
    依次打开Activity1、2、2、1、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

    本文由张风捷特烈原创,转载请注明

    相关文章

      网友评论

        本文标题:2-AI--Activity启动方式

        本文链接:https://www.haomeiwen.com/subject/iqymiftx.html