目录
3.3 使用 Intent 在 Activity 之间穿梭
只有一个 Activity 的应用也太简单了吧?没错,你的追求应该更高一点。不管你想创建多少个 Activity ,方法都和上一节中介绍的是一样的。唯一的问题在于,你在启动器中点击应用的图标只会进入该应用的主Activity ,那么怎样才能由主 Activity 跳转到其他 Activity 呢?我们现在就 一起来看一看。
3.3.1 使用显式 Intent
你应该已经对创建 Activity 的流程比较熟悉了,那我们现在在 ActivityTest 项目中再快速地创 一个 Activity 。
还是右击 com.example.activitytest 包→New→Activity→Empty Activity ,会弹出一个创建 Activity 的对话框,这次我们命名为 SecondActivity ,并勾选 Generate Layout File ,给布局 文件起名为 second_layout ,但不要勾选 Launcher Activity 选项,如 图3.15 所示。
![](https://i-blog.csdnimg.cn/blog_migrate/c6fbb50afe948eb2ed04881ef588b585.png)
点击“Finish” 完成创建,Android Studio 会为我们自动生成 SecondActivity.kt 和 second_layout.xml 这两个文件。不过自动生成的布局代码目前对你来说可能有些难以理解, 这里我们还是使用比较熟悉的 LinearLayout ,编辑 second_layout.xml ,将里面的代码替换成如下内容:
<LinearLayout
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=".SecondActivity">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
我们还是定义了一个按钮,并在按钮上显示Button 2 。
SecondActivity 中的代码已经自动生成了一部分,我们保持默认不变即可,如下所示:
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second_layout)
}
}
另外不要忘记,任何一个 Activity 都是需要在 AndroidManifest.xml 中注册的。不过幸运的 是,Android Studio 已经帮我们自动完成了,你可以打开 AndroidManifest.xml 瞧一瞧:
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ActivityTest"
tools:targetApi="31">
<activity
android:name=".SecondActivity"
android:exported="false" />
<activity
android:name=".FirstActivity"
android:exported="true"
android:label="This is FirstActivity">
<!-- 为程序配置主Activity -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
由于 SecondActivity 不是主 Activity ,因此不需要配置标签里的内容,注册 Activity 的代码也简单了许多。现在第二个 Activity 已经创建完成,剩下的问题就是如何去启动它了,这里我们需要引入一个新的概念:Intent 。
Intent 是 Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据。Intent 一般可用于启动 Activity 、启动 Service 以 及发送广播等场景,由于 Service 、广播等概念你暂时还未涉及,那么本章我们的目光无疑就锁 定在了启动 Activity 上面。
Intent 大致可以分为两种:显式 Intent 和隐式 Intent 。我们先来看一下显式 Intent 如何使用。
Intent 有多个构造函数的重载,其中一个是 Intent(Context packageContext, Class cls)。这个构造函数接收两个参数:第一个参数 Context 要求提供一个启动 Activity 的上下文;第二个参数Class 用于指定想要启动的目标 Activity ,通过这个构造函数就可以构建出 Intent 的“意图”。那么接下来我们应该怎么使用这个 Intent 呢?Activity 类中提供了一个 startActivity() 方法,专门用于启动 Activity ,它接收一个 Intent 参数,这里我们将构建好的 Intent 传入startActivity() 方法就可以启动目标 Activity 了。
修改 FirstActivity 中按钮的点击事件,代码如下所示:
button1.setOnClickListener {
val intent = Intent(this,SecondActivity::class.java)
startActivity(intent)
}
我们首先构建了一个 Intent 对象,第一个参数传入 this 也就是 FirstActivity 作为上下文,第二个参数传入 SecondActivity::class.java 作为目标 Activity ,这样我们的“意图”就非常明显了,即在 FirstActivity 的基础上打开 SecondActivity 。注意,Kotlin 中 SecondActivity::class.java 的写法就相当于 Java 中 SecondActivity.class 的写法。 接下来再通过 startActivity() 方法执行这个 Intent 就可以了。
重新运行程序,在 FirstActivity 的界面点击一下按钮,结果如 图3.16 所示。
![](https://i-blog.csdnimg.cn/blog_migrate/db3dc6cecf9680e39e863dd8d27dc386.png)
可以看到,我们已经成功启动 SecondActivity 了。如果你想要回到上一个 Activity 怎么办呢? 很简单,按一下 Back 键就可以销毁当前 Activity ,从而回到上一个 Activity 了。
使用这种方式来启动 Activity ,Intent 的“意图”非常明显,因此我们称之为显式 Intent 。