Activity的四种启动模式和应用场景?

简介

通过设置ActivityManifest中的Activity的属性launchMode可以设置Activity的启动模式。

默认情况下,使用启动模式:standard。

同时,launchMode可以通过Intent.Flags的改变在运行时被复写,比
如 [Intent]

  • FLAG_ACTIVITY_SINGLE_TOP
  • FLAG_ACTIVITY_NEW_TASK
  • FLAG_ACTIVITY_MULTIPLE_TASK

四种启动模式

standard

默认模式:

  • 每次启动时都会创建一个新的实例

singleTop

栈顶模式:

  • 当启动Activity时,若该Activity位于栈顶,也就是正在前台与用户交互,那就复用这个Activity不会重新创建,该实例Activity.onNewIntent()方法会被调用.
  • 当启动Activity时,若该Activity位于没有位于栈顶则创建和默认默认模式一样重新创建一个新的实例

singleTask

栈内唯一模式:

  • 在启动activity时,若没有该Activity的实例存在于栈内,则会重新创建该Activity的实例.
  • 在启动activity时,若该栈内存在于该Activity的实例并且位于栈顶,则不会重新创建,而是复用该实例,并且会调用Activity.onNewIntent()方法.
  • 在启动activity时,若该栈内存在于该Activity的实例并且没有位于栈顶,那么这个Activity实例会被置为栈顶,并且移除其上面的所有Activity,并调用Activity.onNewIntent()方法

singleInstance

应用内栈唯一模式:
Activity的实例会存在于单独的任务栈内

  • 在启动activity时,若该实例存在则
    其会被置为栈顶并且调用其 Activity.onNewIntent()方法.
  • 在启动activity时,若该实例不存在,则要启动一个新activity实例,并且会存在于一个单独的任务栈中

范例代码

核心代码

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button button;
    protected Button button2;
    protected Button button3;
    protected Button button4;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle(this.getClass().getSimpleName());
        Log.i(TAG, this + ILogTag.ARROW + "onCreate: ");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to StandardActivity");
            startActivity(new Intent(this, StandardActivity.class));
        } else if (view.getId() == R.id.button2) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleTopActivity");
            startActivity(new Intent(this, SingleTopActivity.class));
        } else if (view.getId() == R.id.button3) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleTaskActivity");
            startActivity(new Intent(this, SingleTaskActivity.class));
        } else if (view.getId() == R.id.button4) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleInstanceActivity");
            startActivity(new Intent(this, SingleInstanceActivity.class));
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(MainActivity.this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(MainActivity.this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(MainActivity.this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(MainActivity.this);
    }
}

布局activity_main




    

StandardActivity

public class StandardActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button button;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_standard);
        initView();
        Log.i(TAG, this + ILogTag.ARROW + "onCreate: ".concat(ILogTag.ARROW));
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle(this.getClass().getSimpleName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, this + ILogTag.ARROW + "onDestroy: ".concat(ILogTag.ARROW));
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, this + ILogTag.ARROW + "onStop: ".concat(ILogTag.ARROW));
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Log.i(TAG, this + ":navigate to StandardActivity".concat(ILogTag.ARROW));
            startActivity(new Intent(this, StandardActivity.class));
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(StandardActivity.this);
    }
}

activity_standard





    

StandardActivity启动模式为默认模式


SingleTopActivity

public class SingleTopActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button button;
    protected Button button2;
    protected Button button3;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_single_top);
        initView();
        Log.i(TAG, this + ILogTag.ARROW + "onCreate: ");
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle(this.getClass().getSimpleName());
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, this + ILogTag.ARROW + "onStop: ");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to StandardActivity");
            startActivity(new Intent(this, StandardActivity.class));
        } else if (view.getId() == R.id.button2) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleTopActivity");
            startActivity(new Intent(this, SingleTopActivity.class));
        } else if (view.getId() == R.id.button3) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to TempActivity");
            startActivity(new Intent(this, TempActivity.class));
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(SingleTopActivity.this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(SingleTopActivity.this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(SingleTopActivity.this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, this + ILogTag.ARROW + "onNewIntent: ");
    }
}

activity_single_top





    

SingleTaskActivity

package com.activity.mode;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SingleTaskActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button button;
    protected Button button2;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_single_task);
        initView();
        Log.i(TAG, this + ILogTag.ARROW + "onCreate: ");
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleTaskActivity");
            startActivity(new Intent(this, SingleTaskActivity.class));
        } else if (view.getId() == R.id.button2) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to TempActivity");
            startActivity(new Intent(this, TempActivity.class));
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(SingleTaskActivity.this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(SingleTaskActivity.this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, this + ILogTag.ARROW + "onNewIntent: ");
    }
}

activity_single_task





    

SingleInstanceActivity

package com.activity.mode;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SingleInstanceActivity extends AppCompatActivity implements View.OnClickListener {
    protected Button button;
    protected Button button2;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_single_instance);
        Log.i(TAG, this + ILogTag.ARROW + "onCreate: ");
        initView();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, this + ILogTag.ARROW + "onStop: ");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, this + ILogTag.ARROW + "onNewIntent: ");
    }


    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(SingleInstanceActivity.this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(SingleInstanceActivity.this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleInstanceActivity");
            startActivity(new Intent(this, SingleInstanceActivity.class));
        } else if (view.getId() == R.id.button2) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to TempActivity");
            startActivity(new Intent(this, TempActivity.class));
        }
    }
}

activity_single_instance





    

TempActivity

package com.activity.mode;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class TempActivity extends AppCompatActivity implements View.OnClickListener {

    protected Button button;
    protected Button button2;
    protected Button button3;
    protected Button button4;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_temp);
        initView();
        Log.i(TAG, this + ILogTag.ARROW + "onCreate: ");
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle(this.getClass().getSimpleName());
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, this + ILogTag.ARROW + "onStop: ");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to StandardActivity");
            startActivity(new Intent(this, StandardActivity.class));
        } else if (view.getId() == R.id.button2) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleTopActivity");
            startActivity(new Intent(this, SingleTopActivity.class));
        } else if (view.getId() == R.id.button3) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleTaskActivity");
            startActivity(new Intent(this, SingleTaskActivity.class));
        } else if (view.getId() == R.id.button4) {
            Log.i(TAG, this + ILogTag.ARROW + ":navigate to SingleInstanceActivity");
            startActivity(new Intent(this, SingleInstanceActivity.class));
        }
    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(TempActivity.this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(TempActivity.this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(TempActivity.this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(TempActivity.this);
    }
}

activity_temp





    

AndroidManifest




    
        
            
                

                
            
        
        
        
        
        
        
    


standard模式

操作如下:
MainActivity->StandardActivity->StandardActivity->StandardActivity->Back->Back->Back->Back
日志输出:

I/MainActivity: com.activity.mode.MainActivity@ecc0b9d:navigate to StandardActivity--->
I/StandardActivity: com.activity.mode.StandardActivity@c3d1d6e--->onCreate: --->
I/StandardActivity: com.activity.mode.StandardActivity@c3d1d6e:navigate to StandardActivity--->
I/StandardActivity: com.activity.mode.StandardActivity@67987d0--->onCreate: --->
I/StandardActivity: com.activity.mode.StandardActivity@c3d1d6e--->onStop: --->
I/StandardActivity: com.activity.mode.StandardActivity@67987d0:navigate to StandardActivity--->
I/StandardActivity: com.activity.mode.StandardActivity@d652e30--->onCreate: --->
I/StandardActivity: com.activity.mode.StandardActivity@67987d0--->onStop: --->
I/StandardActivity: 
com.activity.mode.StandardActivity@d652e30--->onStop: --->
com.activity.mode.StandardActivity@d652e30--->onDestroy: --->
I/StandardActivity: 
com.activity.mode.StandardActivity@67987d0--->onStop: --->
com.activity.mode.StandardActivity@67987d0--->onDestroy: --->
I/StandardActivity: 
com.activity.mode.StandardActivity@c3d1d6e--->onStop: --->
com.activity.mode.StandardActivity@c3d1d6e--->onDestroy: --->

示意图:


image.png

分析结果:
standard模式,每次启动,都会创建一个新的实例

使用场景:
standard:普通activity

singleTop模式

操作如下:
MainActivity->SingleTopActivity->SingleTopActivity->SingleTopActivity->TempActivity->SingleTopActivity->SingleTopActivity->Back->Back->Back->
日志输出:

I/MainActivity: com.activity.mode.MainActivity@231fb7b--->onCreate: 
I/MainActivity: com.activity.mode.MainActivity@231fb7b:navigate to SingleTopActivity--->
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->onCreate: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->:navigate to SingleTopActivity
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->onNewIntent: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->:navigate to SingleTopActivity
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->onNewIntent: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->:navigate to TempActivity
I/TempActivity: com.activity.mode.TempActivity@8e210a6--->onCreate: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->onStop: 
I/TempActivity: com.activity.mode.TempActivity@8e210a6:navigate to SingleTopActivity--->
I/SingleTopActivity: com.activity.mode.SingleTopActivity@7c619bf--->onCreate: 
I/TempActivity: com.activity.mode.TempActivity@8e210a6--->onStop: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@7c619bf--->:navigate to SingleTopActivity
I/SingleTopActivity: com.activity.mode.SingleTopActivity@7c619bf--->onNewIntent: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@7c619bf--->onStop: 
I/TempActivity: com.activity.mode.TempActivity@8e210a6--->onStop: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@a9de37d--->onStop: 

示意图:


image.png

分析结果:
singleTop模式,每次启动,若栈顶存在实例就复用,并且回调onNewIntent,若没有就重新创建

使用场景:
singleTop:要展示推送过来的消息

singleTask模式

操作如下:
MainActivity->SingleTaskActivity->SingleTaskActivity->SingleTaskActivity->TempActivity->SingleTopActivity->TempActivity->SingleTaskActivity->Back
日志输出:

I/MainActivity: com.activity.mode.MainActivity@ecc0b9d--->onCreate: 
I/MainActivity: com.activity.mode.MainActivity@ecc0b9d--->:navigate to SingleTaskActivity
I/SingleTaskActivity: com.activity.mode.SingleTaskActivity@c4d1ccc--->:navigate to SingleTaskActivity
I/SingleTaskActivity: com.activity.mode.SingleTaskActivity@c4d1ccc--->onNewIntent: 
I/SingleTaskActivity: com.activity.mode.SingleTaskActivity@c4d1ccc--->:navigate to SingleTaskActivity
I/SingleTaskActivity: com.activity.mode.SingleTaskActivity@c4d1ccc--->onNewIntent: 
I/SingleTaskActivity: com.activity.mode.SingleTaskActivity@c4d1ccc--->:navigate to TempActivity
I/TempActivity: com.activity.mode.TempActivity@35cf06--->onCreate: 
I/TempActivity: com.activity.mode.TempActivity@35cf06--->:navigate to SingleTopActivity
I/SingleTopActivity: com.activity.mode.SingleTopActivity@4ef3897--->onCreate: 
I/TempActivity: com.activity.mode.TempActivity@35cf06--->onStop: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@4ef3897--->:navigate to TempActivity
I/TempActivity: com.activity.mode.TempActivity@7504367--->onCreate: 
I/SingleTopActivity: com.activity.mode.SingleTopActivity@4ef3897--->onStop: 
I/TempActivity: com.activity.mode.TempActivity@7504367--->:navigate to SingleTaskActivity
I/SingleTaskActivity: com.activity.mode.SingleTaskActivity@c4d1ccc--->onNewIntent: 
I/TempActivity: com.activity.mode.TempActivity@7504367--->onStop: 

示意图:


image.png

分析结果:
singleTask模式,每次启动,若栈内部存在实例就复用并且置为栈顶移除其顶部所有的Activity,并且回调onNewIntent,若没有就重新创建实例

使用场景:
singleTask:程序入口等启动页面

singleInstance模式

操作如下:
MainActivity->SingleInstanceActivity->SingleInstanceActivity->SingleInstanceActivity->TempActivity->SingleInstanceActivity->Back->Back
日志输出:

I/MainActivity: com.activity.mode.MainActivity@ecc0b9d--->onCreate: 
I/MainActivity: com.activity.mode.MainActivity@ecc0b9d--->:navigate to SingleInstanceActivity
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->onCreate: 
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->:navigate to SingleInstanceActivity
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->onNewIntent: 
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->:navigate to SingleInstanceActivity
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->onNewIntent: 
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->:navigate to TempActivity
I/TempActivity: com.activity.mode.TempActivity@f138abf--->onCreate: 
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->onStop: 
I/TempActivity: com.activity.mode.TempActivity@f138abf--->:navigate to SingleInstanceActivity
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->onNewIntent: 
I/TempActivity: com.activity.mode.TempActivity@f138abf--->onStop: 
I/SingleInstanceActivity: com.activity.mode.SingleInstanceActivity@72d17cd--->onStop: 
I/TempActivity: com.activity.mode.TempActivity@f138abf--->onStop: 

示意图:


image.png

分析结果:
singleInstance模式,每次启动,若栈内部存在实例就复用并且置为栈顶并且回调onNewIntent,若没有就重新创建实例和一个单独的任务栈!!!

使用场景:
singleInstance:完全独立的,类似闹钟的提示

完成代码

MyActivityModeDemo

你可能感兴趣的:(Activity的四种启动模式和应用场景?)