Android_四大组件之Activity

一、概述

 Activity是应用组件,提供了用户交互的窗口。一个应用由多个彼此联系的Activity组成。它大多数情况是全屏窗口显示,也可以作为悬浮窗口 或者 多窗口模式。

注:以下所有流程图片均来自Android官网。

 

二、生命周期

              图1

上图(图1) 很清晰的显示了Activity的整个生命周期。

onCreate():首次创建Activity时调用,执行所有正常静态设置 -创建视图等,接收系统传过来的Bundle 对象(可以包含上个Activity的状态等参数),后面一定时onStart();

onStart(): 在Activity对用户可见之前调用。 如果Activity转为前台,则后续跟onResume(),  转入后台隐藏则调用onStop();

onResume():  Activity对用户可见 并在可交互之前调用。此时,Activity 处于 Activity 堆栈的顶层,并具有用户输入焦点。

onPause(): 当系统即将开始继续另一个 Activity 时调用,当前Activity失去焦点。

onStop():在 Activity 对用户不再可见时调用;

onDestory():Activity被销毁前调用。

 

Activity生命周期有3给对应的过程:

1.整个生命周期:从onCreate()到onDestory(), 从创建到销毁,从资源申请到释放。

2.可见生命周期:从onStart()到onStop(),这个过程对用户时可见的。在调用这两个方法之间保留向用户显示 Activity 所需的资源。

3.前台生命周期:从onResume()到onPause(),在这段时间,Activity 位于屏幕上的所有其他 Activity 之前,并具有用户输入焦点。

 

举个例子:

这里有3个Activity, MainActivity, SecondActivity,ThirdActiivity. MainActiivty中一个按钮跳转到SecondActivity,同样SecondActivity中仅有一个按钮跳转到ThirdActivity.

下面是MainActivity和SecondActivity的具体代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    final private static String TAG = "MainActivity_flx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "MainActivity onCreate: ");

        Button to2btn = findViewById(R.id.to_second_activity_btn);
        to2btn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        } );
    }

    @Override
    protected void onStart() {
        Log.d(TAG, "MainActivity onStart: ");
        super.onStart();
    }

  @Override protected void onRestart() { Log.d( TAG, "MainActivity onRestart: " ); super.onRestart(); } @Override
protected void onResume() { Log.d(TAG, "MainActivity onResume: "); super.onResume(); } @Override protected void onPause() { Log.d(TAG, "MainActivity onPause: "); super.onPause(); } @Override protected void onStop() { Log.d(TAG, "MainActivity onStop: "); super.onStop(); } @Override protected void onDestroy() { Log.d(TAG, "MainActivity onDestroy: "); super.onDestroy(); } }
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {
    final private static String TAG = "SecondActivity_flx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.d( TAG, "SecondActivity onCreate: " );

        Button to3btn = findViewById( R.id.to_third_btn );
        to3btn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                startActivity(intent);
            }
        } );
    }

    @Override
    protected void onStart() {
        Log.d( TAG, "SecondActivity onStart: " );
        super.onStart();
    }

  @Override protected void onRestart() { Log.d( TAG, "SecondActivity onRestart: " ); super.onRestart(); } @Override
protected void onResume() { Log.d( TAG, "SecondActivity onResume: " ); super.onResume(); } @Override protected void onPause() { Log.d( TAG, "SecondActivity onPause: " ); super.onPause(); } @Override protected void onStop() { Log.d( TAG, "SecondActivity onStop: " ); super.onStop(); } @Override protected void onDestroy() { Log.d( TAG, "SecondActivity onDestroy: " ); super.onDestroy(); } }

下面是一些操作的log,仔细看下就能理解上面的生命周期变化了。

2019-08-08 20:11:46.248 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-08 20:11:46.269 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-08 20:11:46.271 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
---MainActivity刚启动: MainActivity依次执行了onCreate(),onStart(),onResume

2019-08-08 20:12:36.464 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-08 20:12:36.571 26210-26210/com.flx.testactivity D/SecondActivity_flx: SecondActivity onCreate: 
2019-08-08 20:12:36.587 26210-26210/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-08 20:12:36.589 26210-26210/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-08 20:12:37.202 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStop:
---MainAcivity点击跳转到SecondActivity: MainActivity是onPause()->onStop(),失去焦点,SecondActivity启动(如同MainActivity启动)

2019-08-08 20:13:45.641 26210-26210/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-08 20:13:45.664 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onRestart: 
2019-08-08 20:13:45.666 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-08 20:13:45.668 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-08 20:13:46.214 26210-26210/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
2019-08-08 20:13:46.218 26210-26210/com.flx.testactivity D/SecondActivity_flx: SecondActivity onDestroy: 
---按返回键从SecondActivity回到MainActivity

2019-08-08 20:14:51.038 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-08 20:14:51.787 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-08 20:14:51.793 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
---再按返回键。由于是第一个Activity,返回栈最后一个,所以应用退出(下面有讲到)。

2019-08-08 20:16:41.672 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-08 20:16:41.688 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-08 20:16:41.690 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onResume:
---启动应用,进入MainActivity
2019-08-08 20:17:42.492 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 2019-08-08 20:17:42.568 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: ---按Home键,回到桌面。Activity状态 2019-08-08 20:18:22.765 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onRestart: 2019-08-08 20:18:22.783 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 2019-08-08 20:18:22.784 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: ---回到应用 2019-08-08 20:18:43.950 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 2019-08-08 20:18:44.014 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 2019-08-08 20:18:45.591 26210-26210/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: ---通过最近任务栏直接关闭应用。

 

三、任务(Task)和返回栈(Back stack)

Task:    任务是指在执行特定功能操作时与用户交互的一系列 Activity。 这些 Activity 按照各自的打开顺序排列在堆栈(即返回栈)中。

eg:可以是同一个应用,如短信中列表,点击进入查看具体的,里面有各种操作;也可以不同应用的Activity,如短信里需要发邮件,则启动调用email的应用

Back stack:    一个任务的所有Activities依次会放到一个堆栈中,这个stack就是Back stack 。 

 

 

返回栈遵循“后进先出”,当新的Activity被创建后,新的Activity会放到堆栈顶部并获取焦点,之前的Activity会被压入堆栈下一层,但保留用户界面状态。当按返回键时,最上层的Activity会被弹出并销毁,下面的Activity重新弹出到堆栈顶部并显示。

                      图2

图2 就表明了这一过程,一个任务中,新的Activity被创建时如何加入到back stack中的。当用户按返回键后,当前的Activity会被销毁(destory),之前的Actvity会重新回到顶端(Resume)。如生命周期部分中的log的  红色部分显示的过程。

 

当用户开始新的任务时,之前的任务转入后台,任务返回栈中的所有Activity处于Stop状态 失去焦点,但返回栈保持不变,等待恢复返回前台。

        图3

 

四、常见使用

1.启动:

(1)AndroidManifest.xml中声明(必须的),Activity最少包含android:name属性,Activity的名字,包含路径。

<intent-filter>
    <action android:name="android.intent.action.MAIN" />  ---程序主入口
    <category android:name="android.intent.category.LAUNCHER" />  ---和上面一起则能在启动器中显示的入口即能看到的图标
</intent-filter>

一个程序可以有多个入口和图标。

(2)代码中启动: 

以startActivity(intent)最简单的为例(其他参考api)。

intent构造时有多种参数方式,常见的几个

1.Intent intent = new Intent(MainActivity.this, SecondActivity.class);
2.Intent intent = new Intent("xxxaction");
3.Intent intent = new Intent();
  ComponentName com = new ComponentName("com.example.pkgname","com.example.testcomponent.MyActivity");
  intent.setComponent(com);
。。。

 

2.启动模式(简单说明)

Manifest配置,是Activity下的android:launchMode属性,有4中模式,具体入下。

配合简单示例说明,上述代码简单修改,MainActivity一个按钮启动SecondActivity, SecondActivity一个按钮启动MainActivity。

具体代码如下:MainActivity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    final private static String TAG = "MainActivity_flx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "MainActivity onCreate: ");

        Button to2btn = findViewById(R.id.to_second_activity_btn);
        to2btn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( MainActivity.this, SecondActivity.class );
                startActivity( intent );
            }
        } );
    }

    @Override
    protected void onStart() {
        Log.d(TAG, "MainActivity onStart: ");
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Log.d( TAG, "MainActivity onRestart: " );
        super.onRestart();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "MainActivity onResume: ");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d(TAG, "MainActivity onPause: ");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "MainActivity onStop: ");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "MainActivity onDestroy: ");
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d( TAG, "MainActivity onNewIntent: " );
        super.onNewIntent( intent );
    }
}

SecondActivity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends Activity {
    final private static String TAG = "SecondActivity_flx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.d( TAG, "SecondActivity onCreate: " );

        Button toMainBtn = findViewById( R.id.to_main_btn );
        toMainBtn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SecondActivity.this, MainActivity.class);
                startActivity(intent);
            }
        } );
    }

    @Override
    protected void onStart() {
        Log.d( TAG, "SecondActivity onStart: " );
        super.onStart();
    }

    @Override
    protected void onRestart() {
        Log.d( TAG, "SecondActivity onRestart: " );
        super.onRestart();
    }

    @Override
    protected void onResume() {
        Log.d( TAG, "SecondActivity onResume: " );
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d( TAG, "SecondActivity onPause: " );
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d( TAG, "SecondActivity onStop: " );
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d( TAG, "SecondActivity onDestroy: " );
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d( TAG, "SecondActivity onNewIntent: " );
        super.onNewIntent( intent );
    }
}

设置MainActivity的模式,两种启动方式了解不同模式 如何创建实例的。一种通过SecondActivity再启动,一种通过adb启动(模拟其他任务启动)

adb shell am start -n com.flx.testactivity/.MainActivity

 

(1).standard: 0

默认模式,未指定的话则是这种模式。 在任务中创建一个新的Activty实例(不管任务中是否存在)。

系统在启动 Activity 的任务中创建 Activity 的新实例并向其传送 Intent。Activity 可以多次实例化,而每个实例均可属于不同的任务,并且一个任务可以拥有多个实例。

Intent.FLAG_ACTIVITY_NEW_TASK,设置后在历史栈中Activity启动一个新任务。如果Activity的任务已运行,则新的Activity不会被创建,会将那个任务移动到屏幕最上层。

<activity android:name=".MainActivity"
            android:launchMode="standard">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

看看下列操作的表现:

2019-08-09 10:17:51.066 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 10:17:51.085 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:17:51.087 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onResume:
---启动MainActivity,创建实例 

2019-08-09 10:17:55.498 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:17:55.605 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onCreate: 
2019-08-09 10:17:55.620 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-09 10:17:55.622 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-09 10:17:56.241 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
---进入SecondActivity

2019-08-09 10:17:58.075 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-09 10:17:58.159 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 10:17:58.174 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:17:58.175 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 10:17:58.778 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
---SecondActivity中启动MainActivity,再次创建了实例。

2019-08-09 10:18:26.159 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:18:26.246 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStop:
---Home键回到桌面

2019-08-09 10:24:27.891 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 10:24:27.904 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:24:27.906 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
---adb启动MainActivity,再次创建了实例。(模拟其他任务启动的)

2019-08-09 10:25:11.043 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:25:11.080 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onRestart: 
2019-08-09 10:25:11.082 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:25:11.084 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 10:25:11.574 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 10:25:11.577 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
2019-08-09 10:25:12.313 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:25:12.330 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onRestart: 
2019-08-09 10:25:12.333 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-09 10:25:12.334 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-09 10:25:12.873 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 10:25:12.876 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
2019-08-09 10:25:13.221 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-09 10:25:13.243 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onRestart: 
2019-08-09 10:25:13.244 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:25:13.246 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 10:25:13.786 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
2019-08-09 10:25:13.789 9905-9905/com.flx.testactivity D/SecondActivity_flx: SecondActivity onDestroy: 
2019-08-09 10:25:14.121 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:25:14.807 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 10:25:14.818 9905-9905/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
---连续按返回键退出应用,可以看到,按了4次。MainActivity被销毁了3次,即与创建实例次数符合。

从上述看得出,未有特殊处理,standard模式 都会创建一个新的实例

  

(2).singleTop:  1

如果当前任务的Activity实例已经在前台存在了(栈顶),则不会被创建,通过调用onNewIntent()传intent。不会调用onCreate().

Activity 可以多次实例化,而每个实例均可属于不同的任务,并且一个任务可以拥有多个实例(但前提是位于返回栈顶部的 Activity 并不是 Activity 的现有实例,即Activity的实例不是位于栈顶)。

<activity android:name=".MainActivity"
            android:launchMode="singleTop">
2019-08-09 10:47:15.226 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 10:47:15.246 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:47:15.248 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
---启动MainActivity,创建实例 

2019-08-09 10:47:37.641 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:47:37.754 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onCreate: 
2019-08-09 10:47:37.768 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-09 10:47:37.770 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-09 10:47:38.384 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStop:
---进入SecondActivity

2019-08-09 10:47:57.322 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-09 10:47:57.409 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 10:47:57.423 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:47:57.425 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 10:47:58.009 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
---SecondActivity中启动MainActivity,再次创建了实例。

2019-08-09 10:52:54.954 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:52:54.955 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onNewIntent: 
2019-08-09 10:52:54.956 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
---adb启动MainActivity,未创建新的实例(MainActivity在栈顶),调用的onNewIntent

2019-08-09 10:53:39.465 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:53:39.493 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onRestart: 
2019-08-09 10:53:39.495 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-09 10:53:39.496 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-09 10:53:40.069 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 10:53:40.073 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
---返回键,回到了SecondActivity。MainActivity被销毁

2019-08-09 10:54:49.360 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-09 10:54:49.426 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 10:54:49.432 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:54:49.434 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 10:54:50.029 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
---adb启动MainActivity,创建了实例(因为MainActivity不在栈顶了)

2019-08-09 10:57:10.530 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:57:10.550 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onRestart: 
2019-08-09 10:57:10.552 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-09 10:57:10.553 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-09 10:57:11.120 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 10:57:11.126 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
2019-08-09 10:57:11.232 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-09 10:57:11.252 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onRestart: 
2019-08-09 10:57:11.255 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 10:57:11.256 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 10:57:11.794 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
2019-08-09 10:57:11.797 11072-11072/com.flx.testactivity D/SecondActivity_flx: SecondActivity onDestroy: 
2019-08-09 10:57:11.971 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 10:57:12.694 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 10:57:12.698 11072-11072/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
---返回退出,这里销毁了2次,符合的。(总创建3个实例,销毁3次

 

从上述可以看出,singleTop模式,Activity处于栈顶时不会创建实例,会调用onNewIntent()。当不处于栈顶时,则是会创建新的实例

 

(3).singleTask:  2

如果已存在该Activity实例的任务,则会将该任务放到前台,调用onNewIntent()传intent.

 <activity android:name=".MainActivity"
            android:launchMode="singleTask">
2019-08-09 11:15:50.334 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onCreate: 
2019-08-09 11:15:50.352 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 11:15:50.355 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
---启动MainActivity,创建实例 

2019-08-09 11:15:54.858 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 11:15:54.984 12920-12920/com.flx.testactivity D/SecondActivity_flx: SecondActivity onCreate: 
2019-08-09 11:15:54.998 12920-12920/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStart: 
2019-08-09 11:15:55.000 12920-12920/com.flx.testactivity D/SecondActivity_flx: SecondActivity onResume: 
2019-08-09 11:15:55.615 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
---进入SecondActivity

2019-08-09 11:16:16.334 12920-12920/com.flx.testactivity D/SecondActivity_flx: SecondActivity onPause: 
2019-08-09 11:16:16.359 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onNewIntent: 
2019-08-09 11:16:16.360 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onRestart: 
2019-08-09 11:16:16.361 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onStart: 
2019-08-09 11:16:16.363 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
2019-08-09 11:16:16.913 12920-12920/com.flx.testactivity D/SecondActivity_flx: SecondActivity onStop: 
2019-08-09 11:16:16.917 12920-12920/com.flx.testactivity D/SecondActivity_flx: SecondActivity onDestroy: 
---SecondActivity中启动MainActivity,未创建实例,调用onNewIntent。SecondActivity被销毁。

2019-08-09 11:18:30.055 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 11:18:30.056 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onNewIntent: 
2019-08-09 11:18:30.056 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onResume: 
---adb启动MainActivity,未创建实例,调用onNewIntent

2019-08-09 11:19:07.607 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onPause: 
2019-08-09 11:19:08.334 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onStop: 
2019-08-09 11:19:08.338 12920-12920/com.flx.testactivity D/MainActivity_flx: MainActivity onDestroy: 
---返回键退出

 

从上述看到,singleTask模式,仅仅一个实例,如果存在都不会创建实例。

下图图4 显示了如何将启动模式为“singleTask”的 Activity 添加到返回栈。 如果 Activity 已经是某个拥有自己的返回栈的后台任务的一部分,则整个返回栈也会上移到当前任务的顶部。 

所以,如果启动的SingleTask模式的Activity不在Task的顶部,整个Task的back stack移到前台,而处于该Activity前面的Activity则都会被弹出销毁。如上面log中启动已有的MainActivity,SecondActivity被销毁了。

              图4

 

(4).singleInstance:  3

只允许一个实例,独占整个任务。创建时 如果存在了,则将他所在的任务调到前台。

类似singleTask,只是它的任务中不包含任何其他Activity,singleInstance的Activity独占整个个Task,其他和singleTask一样。

 

 

3.Intent过滤器

Activity满足一定条件,系统才会像Activity发送特定intent。

(1).操作:<action>

如果不包含任何action,则不接受任何Intent.

eg:<action android:name="com.example.project.TRANSMOGRIFY" />

TRANSMOGRIFY操作,最好包含包名,确保唯一性。

(2).数据:<data>

<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />

数据规范检测。引用待操作数据和/或该数据 MIME 类型的 URI(Uri 对象)

(3).类别:<category>

一个包含应处理 Intent 组件类型的附加信息的字符串。

eg:<category android:name="android.intent.category.LAUNCHER" /> ---前面的,在系统启动器中列表中显示的。

android.intent.category.BROWSABLE  ---允许本身通过网络浏览器启动,如邮件中的链接点击后 则请求包含该类别的应用。

 

posted @ 2019-06-24 11:39  流浪_归家  阅读(548)  评论(0编辑  收藏  举报