使用Intent进行页面之间的跳转【Intent_1】

这篇具有很好参考价值的文章主要介绍了使用Intent进行页面之间的跳转【Intent_1】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

 

目录

Intent 类型

一、显式Intent方法

二、隐式Intent方法


开发文档链接:Intent 和 Intent 过滤器  |  Android 开发者  |  Android Developers (google.cn)https://developer.android.google.cn/guide/components/intents-filters

Intent是一个消息传递对象,解决Android应用的各项组件之间的通讯,您可以用来从其他应用组件请求操作。尽管 Intent 可以通过多种方式促进组件之间的通信,但其基本用例主要包括以下三个:

  • 启动 Activity,即实现在几个Activity之间的跳转
  • 启动服务
  • 传递广播

Intent 类型

Intent 分为两种类型:

  • 显式 Intent:通过提供目标应用的软件包名称或完全限定的组件类名来指定可处理 Intent 的应用。通常,您会在自己的应用中使用显式 Intent 来启动组件,这是因为您知道要启动的 Activity 或服务的类名。例如,您可能会启动您应用内的新 Activity 以响应用户操作,或者启动服务以在后台下载文件。
  • 隐式 Intent :不会指定特定的组件,而是声明要执行的常规操作,从而允许其他应用中的组件来处理。例如,如需在地图上向用户显示位置,则可以使用隐式 Intent,请求另一具有此功能的应用在地图上显示指定的位置。

本文主要介绍第一个用法,通过显式和隐式分别启动 Activity,即实现在几个Activity之间的跳转。

作好准备工作,新建一个项目,命名为MyAppTest_1。

先来构建两个Activity。分别命名为MainActivity(主界面)和SecondActivity(跳转之后的界面)。

activity_main.xml中添加两个按钮。

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_1"
        android:text="显式Intent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btn_2"
        android:text="隐式Intent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/>

</LinearLayout>

activity_second.xml中添加一个字段以示区分。

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is SecondActivity" />

</LinearLayout>

一、显式Intent方法

1. 1  MainActivity.java  完整代码

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //跳转页面的按钮,显式Intent。
        //通过findViewById获取按钮,并定义变量名为bt_change_1
        Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
        bt_change_1.setOnClickListener((View view) -> {
            //显式Intent
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
        });

    }
}

这是其中的第一种写法。


        //跳转页面的按钮,显式Intent。
        //通过findViewById获取按钮,并定义变量名为bt_change_1
        Button bt_change_1 =(Button)this.findViewById(R.id.btn_1);
        bt_change_1.setOnClickListener((View view) -> {
            //显式Intent
            Intent intent = new Intent(this, SecondActivity.class);
            this.startActivity(intent);
        });

还有第二种写法,将上方代码块替换为如下。


        //跳转页面的按钮,显式Intent。
        //通过findViewById获取按钮,并定义变量名为bt_change_2
        Button bt_change_2 =(Button)this.findViewById(R.id.btn_1);
        bt_change_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });

最后运行界面及结果:

使用Intent进行页面之间的跳转【Intent_1】使用Intent进行页面之间的跳转【Intent_1】

二、隐式Intent方法

隐式 Intent 指定能够在可以执行相应操作的设备上调用任何应用的操作。如果您的应用无法执行该操作而其他应用可以,且您希望用户选取要使用的应用,则使用隐式 Intent 非常有用。

Q:  那么是怎样正确找到并打开目标活动呢?

Intent的发送者在构造Intent对象时,并不需要指定“接收者”,而是通过一定的设置,由系统进行筛选,这有助于解耦。隐式Intent需要借助Intent Filter来实现“筛选”这一过程,并且仅当隐式 Intent 可以通过 Intent 过滤器之一传递时,系统才会将该 Intent 传递给应用组件。
 

意思就是说,当且仅当该 “Intent对象” 与 “在清单文件中为目标活动创建的action和category” 相一致时,即调用该目标活动。

<action>为目标活动地址,而<category>标签则包含了一些附加信息,更精确地指明了当前的活动能够响应的Intent中还可能带有的category。只有<action>和<category>中的内容同时能够匹配上
Intent中指定的action和 category时,这个活动才能响应该Intent。
 


下面是具体的做法。

由此引出,我们要注意,在使用隐式调用时,需要先在AndroidManifest.xml(清单文件)中对需要打开的Activity进行定义。

2.1 AndroidManifest.xml 完整代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapptest_1">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyAppTest_1">

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.myapptest_1.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.myapptest_1.MY_CATEGORY"/>
            </intent-filter>
        </activity>

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

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

    </application>

</manifest>

我们做了什么呢,其他的都没有变,仅添加了以下代码:

        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.myapptest_1.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.myapptest_1.MY_CATEGORY"/>
            </intent-filter>
        </activity>

几个问题:

  1. 为什么在<action android:name="com.example.myapptest_1.ACTION_START"></action>这里,最后这里是有一个</action>的方式结尾,我也尝试了以下,改为以 />结尾程序也是可以跑通的。不知道是不是有什么特别的区别。
  2. 这里的com.example.myapptest_1.是活动所在的地址。需要换成你对应的路径。

    使用Intent进行页面之间的跳转【Intent_1】

  3. 为什么定义了两个 category,其中android.intent.category.DEFAULT是一种默认的category, 在调用startActivity()方法的时候会自动将这个category添加到 Intent中。第二个是自己定义的。
  4. 注意:如果当前组件是Activity,并且没有指定的category,必须加上category并使用默认的DEFAULT!!!
    <category android:name="android.intent.category.DEFAULT" />
    
  5. 在<action>和<category>后面这些全大写的指令是什么意思,这些,可以搜一下对应的源码。只需要跳转页面的话,就按照这个样子写是可以的。
  6. 注意:
  • 一个Activity可以指定多个action属性
  • 一个Activity也可以指定多个category属性
  • 一个Intent只能指定一个action
  • 一个intent可以指定多个category

2. 2  MainActivity.java  完整代码

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //跳转页面的按钮,隐性Intent
        Button bt_second =(Button)this.findViewById(R.id.btn_2);
        bt_second.setOnClickListener((View view) -> {
            //隐性Intent
            Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
            intent.addCategory("com.example.myapptest_1.MY_CATEGORY");
            this.startActivity(intent);
        });

    }
}

几个问题:

若没有指定的category,则不需要添加

intent.addCategory("com.example.myapptest_1.MY_CATEGORY");

只需要如下指定action即可,就是说这样写也是可行的,不会报错,但不建议。

        //跳转页面的按钮,隐性Intent
        Button bt_second =(Button)this.findViewById(R.id.btn_2);
        bt_second.setOnClickListener((View view) -> {
            //隐性Intent
            Intent intent = new Intent("com.example.myapptest_1.ACTION_START");
            this.startActivity(intent);
        });

那么我们跳转到了新的界面怎么返回呢?

下面是返回代码示例,我们在 SecondActivity.java 中编写。

package com.example.myapptest_1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Button gobackBtn = findViewById(R.id.button_back);
        gobackBtn.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 返回上一个页面
                finish();
            }
        });

    }
}

相对应的activity_second.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is SecondActivity" />

    <Button
        android:id="@+id/button_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="back"/>

</LinearLayout>

界面如图。

使用Intent进行页面之间的跳转【Intent_1】文章来源地址https://www.toymoban.com/news/detail-417564.html

到了这里,关于使用Intent进行页面之间的跳转【Intent_1】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 小程序与h5之间的跳转(webview)

    在微信官网中,h5跳小程序并带参是有api的,直接使用wx.miniProgram.navigateTo(url)就可以了,但是项目中往往这个是不够用的,因为我们可能还想着从小程序中执行完一些程序后拿到返回值给返回到h5中,这时这个参数就不好带到h5中了,所以这篇文章就解决了这个 小程序向h5传参

    2024年02月10日
    浏览(32)
  • Android页面跳转(Intent)

    布局 代码 隐式意图 没有明确指定组件名的Intent为隐式意图,系统会根据隐式意图中设置的动作(action)、类别(category)、数据UIL等来匹配最合适的组件。 首先在清单文件中使用意图过滤器设置活动的名字 action android:name=“HomeActivity” / category android:name=“android.intent.categ

    2023年04月09日
    浏览(28)
  • js有哪些常用的跳转页面方法(补)

    在JavaScript中,常用的跳转页面方法包括: 使用location对象的href属性跳转页面: 使用location对象的replace方法跳转页面(不会在浏览器历史记录中留下记录): 使用window对象的open方法打开新窗口或标签页: 使用a标签的click方法模拟点击跳转: 使用setTimeout函数延时跳转页面:

    2024年04月09日
    浏览(26)
  • ARMv8/ARMv9架构下特权程序之间的跳转模型与系统启动探析

    ARMv8和ARMv9架构是ARM公司推出的先进处理器架构,被广泛应用于移动设备、服务器和嵌入式系统。这两个架构的设计旨在提供更高的性能、更好的能效以及更强大的安全性。其中,不同特权程序之间的跳转模型是这一架构中关键的组成部分,对于系统的整体安全性和可靠性具有

    2024年03月16日
    浏览(34)
  • Android Studio:Intent与组件通信实现页面跳转功能

    📌Android Studio 专栏正在持续更新中,案例的原理图解析、各种模块分析💖这里都有哦,同时也欢迎大家订阅专栏,获取更多详细信息哦✊✊✊ ✨个人主页:零小唬的博客主页 🥂欢迎大家 👍点赞 📨评论 🔔收藏 ✨作者简介:20级计算机专业学生一枚,来自宁夏,可能会去

    2024年02月05日
    浏览(44)
  • 微信小程序页面的跳转和导航的配置和vant组件

    结论: navigateTo ,  redirectTo  只能打开非 tabBar 页面。 switchTab  只能打开 tabBar 页面。 reLaunch  可以打开任意页面。 页面底部的 tabBar 由页面决定,即只要是定义为 tabBar 的页面,底部都有 tabBar。 调用页面路由带的参数可以在目标页面的 onLoad 中获取。 (1)当我们使用 redirectTo跳

    2024年02月09日
    浏览(42)
  • 微信小程序开发系列(十一)·小程序页面的跳转设置以及参数传递

    目录 1.  跳转到商品列表 1.1  url: 当前小程序内的跳转链接 1.2  navigate:保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面 1.3  redirect: 关闭当前页面,跳转到应用内的某个页面。但不能跳转到 tabbar 页面 1.4  navigate和redirect的区别 1.5  switchTab:跳转到 t

    2024年04月12日
    浏览(37)
  • Spring-mvc的参数传递与常用注解的解答及页面的跳转方式---综合案例

    目录 一.slf4j--日志 二.常用注解        2.1.@RequestMapping       2.2.@RequestParam       2.3.@RequestBody       2.4.@PathVariable 三.参数的传递 3.1 基础类型 3.2 复杂类型 3.3 @RequestParam 3.4  @PathVariable 3.5 @RequestBody 3.6 增删改查  四.返回值            4.1 void 返回值   4.2 String

    2024年02月09日
    浏览(43)
  • iOS-自定义Intent及ShortCut,能通过快捷指令唤醒APP并跳转到指定页面

    创建完成后,在intents文件中勾选以下target,否则在扩展里,无法访问到前面创建的Intent类 编译程序,Xcode 会自动生成对应的类,我这里的话会生成 LaunchAppIntent 类,下面需要使用 引入上述生成的文件,并写入代理,如果之前未勾选target是无法引入的 在代理中写入,以及点击

    2024年01月23日
    浏览(45)
  • 【微信小程序】通过使用 wx.navigateTo方法进行页面跳转,跳转后的页面中通过一些方式回传值给原页面

    以下是几种常见的回传值的方式: 使用 wx.navigateTo 方法传递参数: 在跳转时,可以在目标页面的 URL 中携带参数,然后在目标页面的 onLoad 方法中获取参数,并在目标页面中进行处理。例如: 在目标页面的 onLoad 方法中获取参数: 使用 wx.navigateBack 方法回传值: 在目标页面中

    2024年02月13日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包