Android —— Activity的四种启动模式

Android —— Activity的四种启动模式,第1张

除了Activity的生命周期外,Activity的启动模式也是一个难点,有时候为了满足项目的特殊需求,就必须使用Activity的启动模式。

在默认情况下,当我们多次启动同一个Activity的时候,系统会创建多个实例并把它们放入任务栈中,但是有些场景重复创建多个实例,是没有必要且浪费资源的,这就需要启动模式来修改系统的默认行为。

下面,我将以理论+实践的形式为大家介绍Activity的启动模式。

standard标准模式singleTop栈顶复用模式singleTask栈内复用模式singleInstance单实例模式

standard标准模式

这是系统的默认启动模式,采用这种模式的Activity无论是否已经存在实例,都会重新创建一个实例,在这种模式下谁启动了这个Activity,那么这个Activity就运行在启动它的那个Activity所在的栈中。

实践:MainActivity 采用 standard 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    
	@Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }
    
    //此处省略重复代码
   	
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 18:37:59.622 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onCreate: 0
2022-04-21 18:37:59.625 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onStart: 0
2022-04-21 18:37:59.626 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onResume: 0
// 点击MainActivity的按钮启动MainActivity
2022-04-21 18:38:03.336 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onPause: 0
2022-04-21 18:38:03.373 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onCreate: 1
2022-04-21 18:38:03.378 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onStart: 1
2022-04-21 18:38:03.379 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onResume: 1
2022-04-21 18:38:03.939 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onStop: 0
2022-04-21 18:38:03.942 4072-4072/com.hnucm.myapplication I/MainActivityLog: 12onSaveInstanceState: 0
singleTop栈顶复用模式

在这种模式下,如果新的Activity已经位于任务栈的栈顶,那么此Activity不会被重新创建,同时它的NewIntent方法将会被回调。如果新Activity的实例已存在但不是位于栈顶,那么新Activity依然会被创建。

实践:MainActivity 采用 singleTop 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    
	@Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }
    
    //此处省略重复代码
   	
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 18:52:30.963 4205-4205/com.hnucm.myapplication I/MainActivityLog: 13onCreate: 0
2022-04-21 18:52:30.968 4205-4205/com.hnucm.myapplication I/MainActivityLog: 13onStart: 0
2022-04-21 18:52:30.970 4205-4205/com.hnucm.myapplication I/MainActivityLog: 13onResume: 0
// 点击MainActivity的按钮启动MainActivity
2022-04-21 18:52:35.601 4205-4205/com.hnucm.myapplication I/MainActivityLog: 13onPause: 0
2022-04-21 18:52:35.603 4205-4205/com.hnucm.myapplication I/MainActivityLog: 13onNewIntent: 0
2022-04-21 18:52:35.604 4205-4205/com.hnucm.myapplication I/MainActivityLog: 13onResume: 0

MainActivity 采用 singleTop 模式,SecondActivity采用 standard 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }

    //此处省略重复代码
    
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,SecondActivity.class);
                startActivity(intent);
        }
    }
}

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "SecondActivityLog";
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.i(TAG, getTaskId() +"onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button2);
        button.setOnClickListener(this);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }

    //此处省略重复代码

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button2:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 18:58:54.961 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onCreate: 0
2022-04-21 18:58:54.964 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onStart: 0
2022-04-21 18:58:54.966 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onResume: 0
// 点击MainActivity的按钮启动SecondActivity
2022-04-21 19:00:13.845 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onPause: 0
2022-04-21 19:00:13.895 4815-4815/com.hnucm.myapplication I/SecondActivityLog: 20onCreate: 0
2022-04-21 19:00:13.898 4815-4815/com.hnucm.myapplication I/SecondActivityLog: 20onStart: 0
2022-04-21 19:00:13.900 4815-4815/com.hnucm.myapplication I/SecondActivityLog: 20onResume: 0
2022-04-21 19:00:14.531 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onStop: 0
2022-04-21 19:00:14.534 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onSaveInstanceState: 0
// 点击SecondActivity的按钮启动MainActivity
2022-04-21 19:00:21.287 4815-4815/com.hnucm.myapplication I/SecondActivityLog: 20onPause: 0
2022-04-21 19:00:21.321 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onCreate: 1
2022-04-21 19:00:21.329 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onStart: 1
2022-04-21 19:00:21.331 4815-4815/com.hnucm.myapplication I/MainActivityLog: 20onResume: 1
2022-04-21 19:00:21.865 4815-4815/com.hnucm.myapplication I/SecondActivityLog: 20onStop: 0
2022-04-21 19:00:21.866 4815-4815/com.hnucm.myapplication I/SecondActivityLog: 20onSaveInstanceState: 0
singleTask栈内复用模式

这是一种单实例模式,在这种模式下,只要Activity在一个栈中存在,那么多次启动此Activity都不会重新创建实例,而是回调onNewIntent()

实践:MainActivity 采用 singleTask 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    
	@Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }
    
    //此处省略重复代码
   	
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 19:05:26.977 4945-4945/com.hnucm.myapplication I/MainActivityLog: 21onCreate: 0
2022-04-21 19:05:26.981 4945-4945/com.hnucm.myapplication I/MainActivityLog: 21onStart: 0
2022-04-21 19:05:26.982 4945-4945/com.hnucm.myapplication I/MainActivityLog: 21onResume: 0
// 点击MainActivity的按钮启动MainActivity
2022-04-21 19:05:29.539 4945-4945/com.hnucm.myapplication I/MainActivityLog: 21onPause: 0
2022-04-21 19:05:29.540 4945-4945/com.hnucm.myapplication I/MainActivityLog: 21onNewIntent: 0
2022-04-21 19:05:29.541 4945-4945/com.hnucm.myapplication I/MainActivityLog: 21onResume: 0

MainActivity 采用 singleTask 模式,SecondActivity采用 standard 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }

    //此处省略重复代码
    
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,SecondActivity.class);
                startActivity(intent);
        }
    }
}

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "SecondActivityLog";
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.i(TAG, getTaskId() +"onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button2);
        button.setOnClickListener(this);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }

    //此处省略重复代码

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button2:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 19:11:04.891 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onCreate: 0
2022-04-21 19:11:04.894 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onStart: 0
2022-04-21 19:11:04.897 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onResume: 0
// 点击MainActivity的按钮启动SecondActivity 
2022-04-21 19:11:18.653 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onPause: 0
2022-04-21 19:11:18.690 5045-5045/com.hnucm.myapplication I/SecondActivityLog: 22onCreate: 0
2022-04-21 19:11:18.694 5045-5045/com.hnucm.myapplication I/SecondActivityLog: 22onStart: 0
2022-04-21 19:11:18.696 5045-5045/com.hnucm.myapplication I/SecondActivityLog: 22onResume: 0
2022-04-21 19:11:19.232 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onStop: 0
2022-04-21 19:11:19.235 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onSaveInstanceState: 0
// 点击SecondActivity的按钮启动MainActivity
2022-04-21 19:11:29.909 5045-5045/com.hnucm.myapplication I/SecondActivityLog: 22onPause: 0
2022-04-21 19:11:29.928 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onRestart: 0
2022-04-21 19:11:29.930 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onStart: 0
2022-04-21 19:11:29.931 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onNewIntent: 0
2022-04-21 19:11:29.932 5045-5045/com.hnucm.myapplication I/MainActivityLog: 22onResume: 0
2022-04-21 19:11:30.498 5045-5045/com.hnucm.myapplication I/SecondActivityLog: 22onStop: 0
2022-04-21 19:11:30.499 5045-5045/com.hnucm.myapplication I/SecondActivityLog: 22onDestroy: 0
singleInstance单实例模式

这是一种加强的singleTask模式,它除了具有singleTask模式的所有特性外,还加强了一点,那就是具有此模式的Activity只能单独的位于一个任务栈中。

实践:MainActivity 采用 singleInstance 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    
	@Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }
    
    //此处省略重复代码
   	
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 19:18:03.183 5193-5193/com.hnucm.myapplication I/MainActivityLog: 23onCreate: 0
2022-04-21 19:18:03.186 5193-5193/com.hnucm.myapplication I/MainActivityLog: 23onStart: 0
2022-04-21 19:18:03.188 5193-5193/com.hnucm.myapplication I/MainActivityLog: 23onResume: 0
// 点击MainActivity的按钮启动MainActivity
2022-04-21 19:18:11.416 5193-5193/com.hnucm.myapplication I/MainActivityLog: 23onPause: 0
2022-04-21 19:18:11.419 5193-5193/com.hnucm.myapplication I/MainActivityLog: 23onNewIntent: 0
2022-04-21 19:18:11.419 5193-5193/com.hnucm.myapplication I/MainActivityLog: 23onResume: 0

MainActivity 采用 singleInstance 模式,SecondActivity采用 standard 模式

// 核心代码展示
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "MainActivityLog";
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, getTaskId() + "onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }

    //此处省略重复代码
    
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                Intent intent = new Intent(this,SecondActivity.class);
                startActivity(intent);
        }
    }
}

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
    final static String TAG = "SecondActivityLog";
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.i(TAG, getTaskId() +"onCreate: " + getIntent().getIntExtra("id",0));
        button = findViewById(R.id.button2);
        button.setOnClickListener(this);
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, getTaskId() +"onStart: " + getIntent().getIntExtra("id",0));
    }

    //此处省略重复代码

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button2:
                Intent intent = new Intent(this,MainActivity.class);
                intent.putExtra("id",1);
                startActivity(intent);
        }
    }
}
// 日志结果展示
// 启动MainActivity
2022-04-21 19:19:25.783 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onCreate: 0
2022-04-21 19:19:25.786 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onStart: 0
2022-04-21 19:19:25.787 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onResume: 0
// 点击MainActivity的按钮启动SecondActivity 
2022-04-21 19:19:34.745 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onPause: 0
2022-04-21 19:19:34.825 5261-5261/com.hnucm.myapplication I/SecondActivityLog: 25onCreate: 0
2022-04-21 19:19:34.868 5261-5261/com.hnucm.myapplication I/SecondActivityLog: 25onStart: 0
2022-04-21 19:19:34.871 5261-5261/com.hnucm.myapplication I/SecondActivityLog: 25onResume: 0
2022-04-21 19:19:35.464 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onStop: 0
2022-04-21 19:19:35.466 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onSaveInstanceState: 0
// 点击SecondActivity的按钮启动MainActivity
2022-04-21 19:19:45.068 5261-5261/com.hnucm.myapplication I/SecondActivityLog: 25onPause: 0
2022-04-21 19:19:45.078 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onRestart: 0
2022-04-21 19:19:45.079 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onStart: 0
2022-04-21 19:19:45.080 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onNewIntent: 0
2022-04-21 19:19:45.081 5261-5261/com.hnucm.myapplication I/MainActivityLog: 24onResume: 0
2022-04-21 19:19:45.802 5261-5261/com.hnucm.myapplication I/SecondActivityLog: 25onStop: 0
2022-04-21 19:19:45.803 5261-5261/com.hnucm.myapplication I/SecondActivityLog: 25onSaveInstanceState: 0

以上就是Activity启动模式的介绍。

欢迎留言指出错误。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/992097.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存