当前位置:   article > 正文

Android四大组件之activity(活动)_android activity

android activity


前言

本文主要是整理Android四大组件之一的activity的相关知识;
其他相关内容可以跳转至 Android学习与巩固查看


一、activity简介

Activity是Android四大组件之一,主要功能就是实现一个可以让用户与程序交互的窗口。一般来说一个activity表示一个与用户交互的界面;

二、activity知识点

1.生命周期

activity的生命周期代表了一个activity从产生到被销毁的整个流程,包含了七个周期,如图:
activity生命周期
1. onCreate()
首次创建 Activity 时调用。在此方法中执行资源绑定,视图创建等静态界面初始化;此时用户不可见。在一个activity的生命周期中只会调用一次。
2. onStart()
当前Activity由用户不可见变为用户可见时调用,但是此时用户还无法进行交互。
3. onResume()
当前Activity状态属于运行状态,此时Activity获得焦点,用户可以与之进行交互。
4.onPause()
当另外一个activity覆盖当前的acitivty时,此时当前activity会进入到onPouse()方法中,当前activity是可见的,但不能与用户交互状态。
5.onStop()
onPouse()方法完成之后,此时activity进入onStop()方法,此时activity对用户是不可见的,但是Activity还未被销毁,在系统内存紧张的情况下,有可能会被系统进行回收。所以一般在当前方法可做资源回收。如果重新启动activity将进入onRestart()方法
6.onDestroy()
此方法回调时表示Activity被销毁,如果需要重启Activity需要重新新建,绑定资源及从onCreate()开始执行。
7.onRestart()
重新启动已经停止但未被销毁的 Activity 时调用。重新将Activity显示出来。调用顺序onPouse()->onStop()->onRestart()->onStart()->onResume().

2.生命周期示例验证

新建项目来看看app的各个状态所经历的生命周期;示例代码如下:

MainActivity

public class MainActivity extends AppCompatActivity {

    private Button btDialog;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("MainActivity","onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        btDialog = (Button) findViewById(R.id.bt_dialog);
        btDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context,Activity1.class);
                startActivity(intent);
            }
        });
    }

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

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

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

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

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

    @Override
    protected void onRestart() {
        Log.e("MainActivity","onRestart()");
        super.onRestart();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

Activity1

/**
 * author xiaohu
 * description:
 * date :2022/7/11 14:40
 */
public class Activity1 extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("Activity1","onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);
    }

    @Override
    protected void onStart() {
        Log.e("Activity1","onStart()");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.e("Activity1","onResume()");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.e("Activity1","onPause()");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.e("Activity1","onStop()");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.e("Activity1","onDestroy()");
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        Log.e("Activity1","onRestart()");
        super.onRestart();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

布局代码如下:

MainActivity

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="next Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

然后我们看看启动流程:
1.启动app时:

2022-07-11 14:48:31.870 2662-2662/com.xiaohu.myapplication E/MainActivity: onCreate()
2022-07-11 14:48:31.919 2662-2662/com.xiaohu.myapplication E/MainActivity: onStart()
2022-07-11 14:48:31.920 2662-2662/com.xiaohu.myapplication E/MainActivity: onResume()
  • 1
  • 2
  • 3

2.跳转至新activity时:

2022-07-11 14:49:15.201 2662-2662/com.xiaohu.myapplication E/MainActivity: onPause()
2022-07-11 14:49:15.587 2662-2662/com.xiaohu.myapplication E/MainActivity: onStop()
  • 1
  • 2

3.返回原activity时:

2022-07-11 14:50:54.168 2662-2662/com.xiaohu.myapplication E/MainActivity: onRestart()
2022-07-11 14:50:54.169 2662-2662/com.xiaohu.myapplication E/MainActivity: onStart()
2022-07-11 14:50:54.169 2662-2662/com.xiaohu.myapplication E/MainActivity: onResume()
  • 1
  • 2
  • 3

4.activity切换到后台但未被销毁时:

2022-07-11 14:53:08.625 2662-2662/com.xiaohu.myapplication E/MainActivity: onPause()
2022-07-11 14:53:08.898 2662-2662/com.xiaohu.myapplication E/MainActivity: onStop()
  • 1
  • 2

5.activity从后台显示到前台时:

2022-07-11 14:53:34.732 2662-2662/com.xiaohu.myapplication E/MainActivity: onRestart()
2022-07-11 14:53:34.732 2662-2662/com.xiaohu.myapplication E/MainActivity: onStart()
2022-07-11 14:53:34.732 2662-2662/com.xiaohu.myapplication E/MainActivity: onResume()
  • 1
  • 2
  • 3

6.退出activity时

2022-07-11 14:55:04.936 2662-2662/com.xiaohu.myapplication E/MainActivity: onPause()
2022-07-11 14:55:05.200 2662-2662/com.xiaohu.myapplication E/MainActivity: onStop()
2022-07-11 14:55:05.200 2662-2662/com.xiaohu.myapplication E/MainActivity: onDestroy()
  • 1
  • 2
  • 3

2.activity的启动模式

1.Standard 标准模式
这是activity的默认启动方式,不需要进行设置。
2.SingleTop栈顶复用模式
如果需要创建的activity在已在栈顶则不重复创建,直接复用栈顶的activity;如果activity在不在栈顶或不在栈内则会新建一个activity入栈。
3.SingleTask栈内复用模式
如果需要创建的activity在栈内,则会将activity上的所有activity出栈(销毁),使得栈内activity变为栈顶activity,再复用该activity。
4.SingleInstance单实例模式
改模式的activity仅仅能单独位于一个任务栈中,每次使用该模式创建activity时都会新生成一个Task用于存放该activity。

3.activity的启动模式示例

下面我们也通过实例来看看activity的几种启动模式;
设置activity的启动模式有两种方法:

1.在 AndroidMainifest 设置

        <!--
        standard:标准模式
        singleTop:栈顶复用模式
        singleTask:栈内复用模式
        singleInstance:单例模式
        如不设置,Activity的启动模式默认为标准模式(standard)
        -->
        <activity android:name=".Activity1"
            android:launchMode="standard"
            />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.通过Intent设置

// FLAG_ACTIVITY_NEW_TASK:通过在Intent中加入FLAG_ACTIVITY_NEW_TASK启动Activity时,将在指定的Activity栈中启动Activity,如果栈中已经存在Activity实例,将复用实例,并且调用onNewIntent方法,这个过程跟singleTask模式下的Activity表现相同。
// FLAG_ACTIVITY_SINGLE_TOP:通过在Intent中加入FLAG_ACTIVITY_SINGLE_TOP启动Activity时,如果栈顶已经存在Activity实例,将复用Activity实例,并且调用onNewIntent方法。否则将重新创建Activity实例。这个过程跟singleTop模式下的Activity表现相同。
//FLAG_ACTIVITY_CLEAR_TOP:如果正在启动的Activity已在当前栈中运行,则不会启动该活动的新实例,而是销毁其上的所有其他活动。如果Activity的启动模式是singleTask,那么将调用onNewIntent方法。如果Activity的启动模式是standard那么会把Activity已经它上面的Activity实例一同销毁,并且重建Activity实例放入栈顶。
Intent inten = new Intent (context,Activity1.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

**如果同时设置两种方式的话Intent的优先级高于在AndroidMainifest 中设置的模式;

1.在 AndroidMainifest 设置的实例

我们先在AndroidMainifest 来设置四种方式来看看具体的情况:
示例代码:

MainActivity

public class MainActivity extends AppCompatActivity {

    private Button bt1;
    private Button bt2;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("MainActivity", this.toString() + " : " + "onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        bt1 = (Button) findViewById(R.id.bt_1);
        bt2 = (Button) findViewById(R.id.bt_2);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context,Activity1.class);
                startActivity(intent);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        Log.e("MainActivity",this.toString() + " : " + "onStart()");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.e("MainActivity",this.toString() + " : " + "onResume()");
        super.onResume();

        Log.e("MainActivity",getTaskId() + " : " + this.toString());
    }

    @Override
    protected void onPause() {
        Log.e("MainActivity",this.toString() + " : " + "onPause()");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.e("MainActivity",this.toString() + " : " + "onStop()");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.e("MainActivity",this.toString() + " : " + "onDestroy()");
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        Log.e("MainActivity",this.toString() + "onRestart()");
        super.onRestart();
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

Activity1

public class Activity1 extends AppCompatActivity {

    private Button bt1;
    private Button bt2;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("Activity1", this.toString() + " : " + "onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);
        context = this;
        bt1 = (Button) findViewById(R.id.bt_1);
        bt2 = (Button) findViewById(R.id.bt_2);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context,MainActivity.class);
                startActivity(intent);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context,Activity1.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        Log.e("Activity1", this.toString() + " : " + "onStart()");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.e("Activity1", this.toString() + " : " + "onResume()");
        super.onResume();
        Log.e("Activity1",getTaskId() + " : " + this.toString());
    }

    @Override
    protected void onPause() {
        Log.e("Activity1", this.toString() + " : " + "onPause()");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.e("Activity1", this.toString() + " : " + "onStop()");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.e("Activity1", this.toString() + " : " + "onDestroy()");
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        Log.e("Activity1", this.toString() + " : " + "onRestart()");
        super.onRestart();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity1"
        app:layout_constraintBottom_toTopOf="@id/bt_2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bt_1" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

activity_1.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MainActivity"
        app:layout_constraintBottom_toTopOf="@id/bt_2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activity1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/bt_1" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

MainActivity界面
在这里插入图片描述

Activity1界面

在这里插入图片描述

1.Standard 标准模式:
在AndroidMainfast中设置:

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--
        standard:标准模式
        singleTop:栈顶复用模式
        singleTask:栈内复用模式
        singleInstance:单例模式
        如不设置,Activity的启动模式默认为标准模式(standard)
        -->
        <activity android:name=".Activity1"
            android:launchMode="standard"
            />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

如上图,进入MainActivity后点击两次MAINACTIVITY按钮打印日志如下:

2022-07-11 15:57:56.261 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@38abf22 : onCreate()
2022-07-11 15:57:56.312 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@38abf22 : onStart()
2022-07-11 15:57:56.312 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@38abf22 : onResume()
2022-07-11 15:57:56.312 4988-4988/com.xiaohu.myapplication E/MainActivity: 26 : com.xiaohu.myapplication.MainActivity@38abf22
2022-07-11 15:58:33.583 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@38abf22 : onPause()
2022-07-11 15:58:33.652 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@c131345 : onCreate()
2022-07-11 15:58:33.657 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@c131345 : onStart()
2022-07-11 15:58:33.658 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@c131345 : onResume()
2022-07-11 15:58:33.658 4988-4988/com.xiaohu.myapplication E/MainActivity: 26 : com.xiaohu.myapplication.MainActivity@c131345
2022-07-11 15:58:34.074 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@38abf22 : onStop()
2022-07-11 15:58:35.416 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@c131345 : onPause()
2022-07-11 15:58:35.485 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@bf77c80 : onCreate()
2022-07-11 15:58:35.490 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@bf77c80 : onStart()
2022-07-11 15:58:35.490 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@bf77c80 : onResume()
2022-07-11 15:58:35.491 4988-4988/com.xiaohu.myapplication E/MainActivity: 26 : com.xiaohu.myapplication.MainActivity@bf77c80
2022-07-11 15:58:35.844 4988-4988/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@c131345 : onStop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

我们可以看出在standard模式下我们每次创建activity都会生成一个新的实例加入同一个activity栈中,不会新建activity栈;

2.SingleTop栈顶复用模式

在AndroidMainfast中设置:

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:label="MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--
        standard:标准模式
        singleTop:栈顶复用模式
        singleTask:栈内复用模式
        singleInstance:单例模式
        如不设置,Activity的启动模式默认为标准模式(standard)
        -->
        <activity android:name=".Activity1"
            android:launchMode="singleTop"
            android:label="Activity1"
            />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

进入MainActivity后点击两次ACTIVITY1按钮打印日志如下:

2022-07-12 09:17:31.728 2356-2356/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@b25aea : onCreate()
2022-07-12 09:17:31.776 2356-2356/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@b25aea : onStart()
2022-07-12 09:17:31.776 2356-2356/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@b25aea : onResume()
2022-07-12 09:17:31.777 2356-2356/com.xiaohu.myapplication E/MainActivity: 5 : com.xiaohu.myapplication.MainActivity@b25aea
2022-07-12 09:17:42.382 2356-2356/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@b25aea : onPause()
2022-07-12 09:17:42.451 2356-2356/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@10ac362 : onCreate()
2022-07-12 09:17:42.456 2356-2356/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@10ac362 : onStart()
2022-07-12 09:17:42.457 2356-2356/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@10ac362 : onResume()
2022-07-12 09:17:42.457 2356-2356/com.xiaohu.myapplication E/Activity1: 5 : com.xiaohu.myapplication.Activity1@10ac362
2022-07-12 09:17:42.862 2356-2356/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@b25aea : onStop()
2022-07-12 09:17:44.839 2356-2356/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@10ac362 : onPause()
2022-07-12 09:17:44.839 2356-2356/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@10ac362 : onResume()
2022-07-12 09:17:44.840 2356-2356/com.xiaohu.myapplication E/Activity1: 5 : com.xiaohu.myapplication.Activity1@10ac362
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

我们可以看出在第二次生成activity1的时候,由于activity1在栈顶所以再次生成时是复用了栈顶的activity,并没有重复生成实例;

3.SingleTask栈内复用模式

在AndroidMainfast中设置:

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTask"
            android:label="MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--
        standard:标准模式
        singleTop:栈顶复用模式
        singleTask:栈内复用模式
        singleInstance:单例模式
        如不设置,Activity的启动模式默认为标准模式(standard)
        -->
        <activity android:name=".Activity1"
            android:launchMode="singleTask"
            android:label="Activity1"
            />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

进入MainActivity后 点击ACTIVITY1按钮 再点击MainActivity按钮 再点击ACTIVITY1按钮打印日志如下:

2022-07-12 09:24:53.745 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onCreate()
2022-07-12 09:24:53.750 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onStart()
2022-07-12 09:24:53.750 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onResume()
2022-07-12 09:24:53.751 2586-2586/com.xiaohu.myapplication E/MainActivity: 7 : com.xiaohu.myapplication.MainActivity@951cd54
2022-07-12 09:25:01.540 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onPause()
2022-07-12 09:25:01.580 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@161ebac : onCreate()
2022-07-12 09:25:01.586 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@161ebac : onStart()
2022-07-12 09:25:01.587 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@161ebac : onResume()
2022-07-12 09:25:01.587 2586-2586/com.xiaohu.myapplication E/Activity1: 7 : com.xiaohu.myapplication.Activity1@161ebac
2022-07-12 09:25:01.840 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onStop()
2022-07-12 09:25:04.824 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@161ebac : onPause()
2022-07-12 09:25:04.877 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54onRestart()
2022-07-12 09:25:04.877 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onStart()
2022-07-12 09:25:04.877 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onResume()
2022-07-12 09:25:04.877 2586-2586/com.xiaohu.myapplication E/MainActivity: 7 : com.xiaohu.myapplication.MainActivity@951cd54
2022-07-12 09:25:05.065 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@161ebac : onStop()
2022-07-12 09:25:05.065 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@161ebac : onDestroy()
2022-07-12 09:25:06.199 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onPause()
2022-07-12 09:25:06.251 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@7ac2e59 : onCreate()
2022-07-12 09:25:06.256 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@7ac2e59 : onStart()
2022-07-12 09:25:06.257 2586-2586/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@7ac2e59 : onResume()
2022-07-12 09:25:06.258 2586-2586/com.xiaohu.myapplication E/Activity1: 7 : com.xiaohu.myapplication.Activity1@7ac2e59
2022-07-12 09:25:06.512 2586-2586/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@951cd54 : onStop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

我们可以看出我们总共生成了四个activity,因为栈内复用所以再次生成mainActivity时复用了栈内的mainActivity,但是此时在MainActivity上层的activity会出栈被销毁,所以我们再次生成Activity1是可以看到是新建了一个实例;

4.SingleInstance单实例模式

在AndroidMainfast中设置:

<activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleInstance"
            android:label="MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!--
        standard:标准模式
        singleTop:栈顶复用模式
        singleTask:栈内复用模式
        singleInstance:单例模式
        如不设置,Activity的启动模式默认为标准模式(standard)
        -->
        <activity android:name=".Activity1"
            android:launchMode="singleInstance"
            android:label="Activity1"
            />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

进入MainActivity后 点击ACTIVITY1按钮 再点击MainActivity按钮 再点击MainActivity按钮打印日志如下:

2022-07-12 09:37:15.221 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onCreate()
2022-07-12 09:37:15.226 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onStart()
2022-07-12 09:37:15.227 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onResume()
2022-07-12 09:37:15.227 2776-2776/com.xiaohu.myapplication E/MainActivity: 10 : com.xiaohu.myapplication.MainActivity@775fae1
2022-07-12 09:37:19.573 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onPause()
2022-07-12 09:37:19.661 2776-2776/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@6705381 : onCreate()
2022-07-12 09:37:19.666 2776-2776/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@6705381 : onStart()
2022-07-12 09:37:19.666 2776-2776/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@6705381 : onResume()
2022-07-12 09:37:19.667 2776-2776/com.xiaohu.myapplication E/Activity1: 11 : com.xiaohu.myapplication.Activity1@6705381
2022-07-12 09:37:19.844 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onStop()
2022-07-12 09:37:22.665 2776-2776/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@6705381 : onPause()
2022-07-12 09:37:22.744 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1onRestart()
2022-07-12 09:37:22.744 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onStart()
2022-07-12 09:37:22.744 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onResume()
2022-07-12 09:37:22.745 2776-2776/com.xiaohu.myapplication E/MainActivity: 10 : com.xiaohu.myapplication.MainActivity@775fae1
2022-07-12 09:37:22.984 2776-2776/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@6705381 : onStop()
2022-07-12 09:37:23.851 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onPause()
2022-07-12 09:37:23.851 2776-2776/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@775fae1 : onResume()
2022-07-12 09:37:23.851 2776-2776/com.xiaohu.myapplication E/MainActivity: 10 : com.xiaohu.myapplication.MainActivity@775fae1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

我们可以看到在新建Activity1时同时新建了一个栈用来存放activity1,新建MainActivity时会复用MainActivity存储栈中的实例,而不是新建,所以在该模式下一个栈只存放一个activity,没次新建一个新的activity都会生成一个栈用来存放改activity,如果该activity已经创建过,那么就会复用已创建的activity,不再新建;

4.activity间的数据传递与回传

跳转activity时可以使用 startActivity 或 startActivityForResult 两种方式跳转 然后我们可以定义一个Intent 携带需要传输的数据传递给跳转到的activity,也可以使用bundle注入数据,在通过intent携带bundle来进行传输;我更喜欢后者的携带数据的方式;startActivity只是将数据传递给下一个activity,如果需要接受返回数据就需要使用startActivityForResult ;

1.单方面页面间传值 startActivity

传递数据

                Intent intent = new Intent(context,Activity1.class);
                Bundle bundle = new Bundle();
                bundle.putString("value","hello! this is MainActivity");
                intent.putExtra("bundle",bundle);
                startActivity(intent);
  • 1
  • 2
  • 3
  • 4
  • 5

接收数据

Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        Log.e("Activity1", bundle.get("value").toString());
  • 1
  • 2
  • 3

通过日志打印获取数据

2022-07-21 15:54:05.867 2608-2608/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onPause()
2022-07-21 15:54:05.936 2608-2608/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onCreate()
2022-07-21 15:54:05.941 2608-2608/com.xiaohu.myapplication E/Activity1: hello! this is MainActivity
2022-07-21 15:54:05.941 2608-2608/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onStart()
2022-07-21 15:54:05.942 2608-2608/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onResume()
2022-07-21 15:54:05.942 2608-2608/com.xiaohu.myapplication E/Activity1: 5 : com.xiaohu.myapplication.Activity1@bbb44b4
2022-07-21 15:54:06.369 2608-2608/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onStop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

1.获取返回数据的页面间传值 startActivityForResult

MainActivity传递数据给activity1

Intent intent = new Intent(context,Activity1.class);
                Bundle bundle = new Bundle();
                bundle.putString("value","hello! this is MainActivity");
                intent.putExtra("bundle",bundle);
                startActivityForResult(intent,001);
  • 1
  • 2
  • 3
  • 4
  • 5

MainActivity获取下一个activity1返回的数据

// 获取返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 001){
            Bundle bundle = data.getBundleExtra("backBundle");
            Log.e("MainActivity",bundle.getString("backValue"));
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

activity1获取MainActivity的数据并返回数据给MainActivity

//获取数据
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        Log.e("Activity1", bundle.get("value").toString());


        // 设置返回数据
        Bundle bundleBack = new Bundle();
        bundleBack.putString("backValue","hello! this is Activity1 back");
        intent.putExtra("backBundle",bundleBack);
        setResult(001,intent);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

日志打印整个流程

2022-07-21 16:01:11.571 2872-2872/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onPause()
2022-07-21 16:01:11.638 2872-2872/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onCreate()
2022-07-21 16:01:11.642 2872-2872/com.xiaohu.myapplication E/Activity1: hello! this is MainActivity
2022-07-21 16:01:11.643 2872-2872/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onStart()
2022-07-21 16:01:11.643 2872-2872/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onResume()
2022-07-21 16:01:11.643 2872-2872/com.xiaohu.myapplication E/Activity1: 7 : com.xiaohu.myapplication.Activity1@bbb44b4
2022-07-21 16:01:12.009 2872-2872/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onStop()
2022-07-21 16:01:13.626 2872-2872/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onPause()
2022-07-21 16:01:13.627 2872-2872/com.xiaohu.myapplication E/MainActivity: hello! this is Activity1 back
2022-07-21 16:01:13.627 2872-2872/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fconRestart()
2022-07-21 16:01:13.627 2872-2872/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onStart()
2022-07-21 16:01:13.627 2872-2872/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onResume()
2022-07-21 16:01:13.628 2872-2872/com.xiaohu.myapplication E/MainActivity: 7 : com.xiaohu.myapplication.MainActivity@3f138fc
2022-07-21 16:01:13.764 2872-2872/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onStop()
2022-07-21 16:01:13.764 2872-2872/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@bbb44b4 : onDestroy()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

不仅仅是String类型的数据其他类型的数据也可以这样携带,实体对象也可以序列化之后直接写带传输;
示例如下:
MainActivity代码:

// 设置传值
                Data data = new Data();
                data.setContent("hello! this is MainActivity");

                Intent intent = new Intent(context,Activity1.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("value",data);
                intent.putExtra("bundle",bundle);
                startActivityForResult(intent,001);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
// 获取返回数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 001){
            Bundle bundle = data.getBundleExtra("backBundle");
            Data data1 = (Data) bundle.getSerializable("backValue");
            Log.e("MainActivity",data1.getContent());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Activity1代码

 //获取数据
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        Data data = (Data) bundle.getSerializable("value");
        Log.e("Activity1",data.getContent());


        // 设置返回数据
        data.setContent("hello! this is Activity1 back");
        Bundle bundleBack = new Bundle();
        bundleBack.putSerializable("backValue",data);
        intent.putExtra("backBundle",bundleBack);
        setResult(001,intent);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

日志打印:

2022-07-21 16:07:32.940 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onCreate()
2022-07-21 16:07:32.940 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onCreate()
2022-07-21 16:07:32.993 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onStart()
2022-07-21 16:07:32.994 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onResume()
2022-07-21 16:07:32.994 3044-3044/com.xiaohu.myapplication E/MainActivity: 8 : com.xiaohu.myapplication.MainActivity@3f138fc
2022-07-21 16:07:36.733 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onPause()
2022-07-21 16:07:36.775 3044-3044/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@5d095d9 : onCreate()
2022-07-21 16:07:36.780 3044-3044/com.xiaohu.myapplication E/Activity1: hello! this is MainActivity
2022-07-21 16:07:36.780 3044-3044/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@5d095d9 : onStart()
2022-07-21 16:07:36.781 3044-3044/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@5d095d9 : onResume()
2022-07-21 16:07:36.781 3044-3044/com.xiaohu.myapplication E/Activity1: 8 : com.xiaohu.myapplication.Activity1@5d095d9
2022-07-21 16:07:37.036 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onStop()
2022-07-21 16:07:39.160 3044-3044/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@5d095d9 : onPause()
2022-07-21 16:07:39.162 3044-3044/com.xiaohu.myapplication E/MainActivity: hello! this is Activity1 back
2022-07-21 16:07:39.162 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fconRestart()
2022-07-21 16:07:39.162 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onStart()
2022-07-21 16:07:39.162 3044-3044/com.xiaohu.myapplication E/MainActivity: com.xiaohu.myapplication.MainActivity@3f138fc : onResume()
2022-07-21 16:07:39.162 3044-3044/com.xiaohu.myapplication E/MainActivity: 8 : com.xiaohu.myapplication.MainActivity@3f138fc
2022-07-21 16:07:39.315 3044-3044/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@5d095d9 : onStop()
2022-07-21 16:07:39.315 3044-3044/com.xiaohu.myapplication E/Activity1: com.xiaohu.myapplication.Activity1@5d095d9 : onDestroy()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

以上就是Activity的基本数据传输方法;

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号