Android Activity四种加载方式

Activity启动方式有四种,分别是:

  • standard,默认模式,每次都创建新的Activity,无论是否与原Activity的相同
  • singleTop,允许一个Activity实例化多个对象,与standard唯一的不同是,当将要启动的Activity位于栈顶时,不会创建新的实例,而是直接复用已有实例,调用onNewIntent方法
  • singleTask,只有一个实例。在同一个应用程序中启动他的时候,若Activity不存在,则会在当前task创建一个新的实例,若存在,则会把task中在其之上的其它Activity destory掉并调用它的onNewIntent方法。
  • singleInstance,会使用一个全新的Task栈来存放该Activity实例,也就是说,Activity独占一个Task栈,只会存在一个这样的Activity实例

1. standard

        <activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, ">>> onCreate: " + getTaskId());
        button = findViewById(R.id.main_stand);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, ">>> onDestroy: " + getTaskId());
    }
}

Android Activity四种加载方式_第1张图片

在原来的Activity上点击新建,无论是否相同都会创建实例

2. singleTop

        <activity
            android:name=".MainActivity2"
            android:launchMode="singleTop" />
        <activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button button;
    private Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, ">>> onCreate: " + getTaskId());
        button = findViewById(R.id.main_stand);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
        });
        button2 = findViewById(R.id.main_singleTop);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity2.class));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, ">>> onDestroy: " + getTaskId());
    }
}

Android Activity四种加载方式_第2张图片

当相同的Activity位于栈顶,不会新建,不位于栈顶时则新建

singleTask

        <activity
            android:name=".MainActivity2"
            android:launchMode="singleTask" />
        <activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button button;
    private Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, ">>> onCreate: " + getTaskId());
        button = findViewById(R.id.main_stand);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
        });
        button2 = findViewById(R.id.main_singleTask);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity2.class));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, ">>> onDestroy: " + getTaskId());
    }
}

Android Activity四种加载方式_第3张图片

当实例不存在时创建;当存在且在栈顶时,复用;当存在且不在栈顶时,销毁之上的Activity,让目标位置栈顶

4. singleInstance

        <activity
            android:name=".MainActivity2"
            android:launchMode="singleInstance" />
        <activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Button button;
    private Button button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, ">>> onCreate: " + getTaskId());
        button = findViewById(R.id.main_stand);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
        });
        button2 = findViewById(R.id.main_singleInstance);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MainActivity2.class));
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, ">>> onDestroy: " + getTaskId());
    }
}

Android Activity四种加载方式_第4张图片

退栈时,首先在当前Activity所在的Task栈进行退栈操作,而不是按照进栈的顺序进行退栈,即先把当前Task出栈完成,再出栈其他Task.

作者:氷泠
链接:https://www.imooc.com/article/302444
来源:慕课网

Reference
https://www.imooc.com/article/302444
https://www.cnblogs.com/foxy/p/7660278.html
https://www.cnblogs.com/androidWuYou/p/5887807.html

你可能感兴趣的:(Android开发)