1-2.Jetpack 之 Navigation 跳转编码模板

news2024/9/20 16:50:47

一、Navigation

1、Navigation 概述
  • Navigation 是 Jetpack 中的一个重要成员,它主要是结合导航图(Navigation Graph)来控制和简化 Fragment 之间的导航,即往哪里走,该怎么走
2、Navigate 引入
  • 在模块级 build.gradle 中引入相关依赖
implementation 'androidx.navigation:navigation-fragment:2.3.5'
implementation 'androidx.navigation:navigation-ui:2.3.5'

二、Navigation 跳转实例实操

  • 有三个 Fragment 实现 Navigation 跳转,它们分别是 MoreJumpOneFragment、MoreJumpTwoFragment、MoreJumpThreeFragment
  1. 在 MoreJumpOneFragment 中,可以前进到 MoreJumpTwoFragment

  2. 在 MoreJumpTwoFragment 中,可以前进到 MoreJumpThreeFragment,也可以后退到 MoreJumpOneFragment

  3. 在 MoreJumpThreeFragment 中,可以后退到 MoreJumpTwoFragment,也可以直接后退到 MoreJumpOneFragment

2、具体实现
(1)Fragment Layout
  1. fragment_more_jump_one.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=".fragment.MoreJumpOneFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MoreJumpOneFragment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前进"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. fragment_more_jump_two.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=".fragment.MoreJumpTwoFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MoreJumpTwoFragment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="前进"
        app:layout_constraintBottom_toTopOf="@+id/btn_back"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="后退"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
  1. fragment_more_jump_three.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=".fragment.MoreJumpThreeFragment">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MoreJumpThreeFragment"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="后退"
        app:layout_constraintBottom_toTopOf="@+id/btn_back_to_one"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn_back_to_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="后退到 one"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
(2)Fragment Code
  1. MoreJumpOneFragment.java
package com.my.navigation.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.my.navigation.R;

public class MoreJumpOneFragment extends Fragment {

    private NavController navController;

    private Button btnGo;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more_jump_one, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        navController = Navigation.findNavController(view);

        btnGo = view.findViewById(R.id.btn_go);

        btnGo.setOnClickListener(v -> navController.navigate(R.id.action_moreJumpOneFragment_to_moreJumpTwoFragment));
    }
}
  1. MoreJumpTwoFragment.java
package com.my.navigation.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.my.navigation.R;

public class MoreJumpTwoFragment extends Fragment {

    private NavController navController;

    private Button btnGo;
    private Button btnBack;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more_jump_two, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        navController = Navigation.findNavController(view);

        btnGo = view.findViewById(R.id.btn_go);
        btnBack = view.findViewById(R.id.btn_back);

        btnGo.setOnClickListener(v -> navController.navigate(R.id.action_moreJumpTwoFragment_to_moreJumpThreeFragment));

        btnBack.setOnClickListener(v -> navController.navigateUp());
    }
}
  1. MoreJumpThreeFragment.java
package com.my.navigation.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.my.navigation.R;

public class MoreJumpThreeFragment extends Fragment {

    private NavController navController;

    private Button btnBack;
    private Button btnBackToOne;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more_jump_three, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        navController = Navigation.findNavController(view);

        btnBack = view.findViewById(R.id.btn_back);
        btnBackToOne = view.findViewById(R.id.btn_back_to_one);

        btnBack.setOnClickListener(v -> navController.navigateUp());

        btnBackToOne.setOnClickListener(v -> navController.popBackStack(R.id.moreJumpOneFragment, false));
    }
}
(3)Navigation Graph
  • my_graph_more_jump.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/my_graph_more_jump"
    app:startDestination="@id/moreJumpOneFragment">

    <fragment
        android:id="@+id/moreJumpOneFragment"
        android:name="com.my.navigation.fragment.MoreJumpOneFragment"
        android:label="fragment_more_jump_one"
        tools:layout="@layout/fragment_more_jump_one">
        <action
            android:id="@+id/action_moreJumpOneFragment_to_moreJumpTwoFragment"
            app:destination="@id/moreJumpTwoFragment" />
    </fragment>
    <fragment
        android:id="@+id/moreJumpTwoFragment"
        android:name="com.my.navigation.fragment.MoreJumpTwoFragment"
        android:label="fragment_more_jump_two"
        tools:layout="@layout/fragment_more_jump_two">
        <action
            android:id="@+id/action_moreJumpTwoFragment_to_moreJumpThreeFragment"
            app:destination="@id/moreJumpThreeFragment" />
    </fragment>
    <fragment
        android:id="@+id/moreJumpThreeFragment"
        android:name="com.my.navigation.fragment.MoreJumpThreeFragment"
        android:label="fragment_more_jump_three"
        tools:layout="@layout/fragment_more_jump_three">
        <action
            android:id="@+id/action_moreJumpThreeFragment_to_moreJumpOneFragment"
            app:destination="@id/moreJumpOneFragment" />
    </fragment>
</navigation>
(4)Activity Layout
  • activity_more_jump.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=".MoreJumpActivity">

    <fragment
        android:id="@+id/nhf"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navGraph="@navigation/my_graph_more_jump" />
</androidx.constraintlayout.widget.ConstraintLayout>
(5)Activity Code
  • MoreJumpActivity.java
package com.my.navigation;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MoreJumpActivity extends AppCompatActivity {

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

三、Navigation 跳转详解

1、Navigation 跳转方法
  1. void navigate(@IdRes int resId):该方法用于根据在导航图中定义的 action 的 ID 进行 Fragment 跳转

  2. boolean navigateUp():该方法用于返回到上一个 Fragment

  3. boolean popBackStack(@IdRes int destinationId, boolean inclusive):该方法用于活动栈中弹出 Fragment,直到到达指定的 Fragment,如果 inclusive 被设置为 true,则指定的 Fragment 也会被从活动栈中移除

2、Navigation 活动栈解析
  1. 一开始在 MoreJumpOneFragment 中时,此时的活动栈
MoreJumpOneFragment
  1. 当在 MoreJumpOneFragment 中,前进到 MoreJumpTwoFragment 时,此时的活动栈
MoreJumpTwoFragment
MoreJumpOneFragment
  1. 当在 MoreJumpTwoFragment 中,前进到 MoreJumpThreeFragment 时,此时的活动栈
MoreJumpThreeFragment
MoreJumpTwoFragment
MoreJumpOneFragment
  1. 当在 MoreJumpThreeFragment 中,后退到 MoreJumpOneFragment 时,此时的活动栈
MoreJumpThreeFragment

四、Navigation 跳转补充

1、Navigation 跳转方法补充
  • 在 MoreJumpThreeFragment 中,直接后退到 MoreJumpOneFragme,除了使用 popBackStack 方法,还可以用如下方法
NavOptions navOptions = new NavOptions.Builder().setPopUpTo(R.id.moreJumpOneFragment, true).build();
navController.navigate(R.id.action_moreJumpThreeFragment_to_moreJumpOneFragment, null, navOptions);
2、Navigation 跳转方法解析
NavOptions navOptions = new NavOptions.Builder().setPopUpTo(R.id.moreJumpOneFragment, true).build();
  • 使用 NavOptions.Builder 创建一个 NavOptions 对象,它是跳转行为
  1. 该对象指定了在导航到 moreJumpOneFragment 时,清除活动栈中所有 moreJumpOneFragment 之上的 Fragment

  2. 其中,Builder setPopUpTo(@IdRes int destinationId, boolean inclusive) 方法中的 inclusive 被设置为 true,表示包括 moreJumpOneFragment 本身也被清除

  3. 因为要跳转到 moreJumpOneFragment,所以并不会真正导致 moreJumpOneFragment 从活动栈中被清楚,但所有在它之上的 Fragment 都会被清除,导致看起来就像是直接跳转到了moreJumpOneFragment

navController.navigate(R.id.action_moreJumpThreeFragment_to_moreJumpOneFragment, null, navOptions);
  • 通过 void navigate(@IdRes int resId, @Nullable Bundle args, @Nullable NavOptions navOptions) 方法进行跳转,需要传递 3 个参数,分别为导航图中定义的 action 的 ID、携带数据、跳转行为

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2149448.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《关节机械臂:智能仓库的高效助手》

关节机械臂作为一种高度精密的机器设备&#xff0c;在现代制造工厂的智能仓库系统中发挥着至关重要的作用。其设计初衷便是精准地模拟人类手臂在订单拣选操作中的各种运动&#xff0c;以实现高效、准确的物流作业。 这些多功能的机器人由多个关节巧妙组合而成&#xff0c;通常拥…

人工智能【AI】:未来的驱动力

在21世纪&#xff0c;人工智能&#xff08;AI&#xff09;已经成为推动技术进步和创新的关键力量。AI不仅仅是科幻小说中的概念&#xff0c;它已经渗透到我们日常生活的方方面面&#xff0c;从智能手机的语音助手到复杂的医疗诊断系统&#xff0c;AI的应用无处不在。 人工智能…

KG Structure as Prompt:利用知识图谱构建Prompt,提高大模型对因果关系的理解

KG Structure as Prompt&#xff1a;利用知识图谱构建Prompt&#xff0c;提高大模型对因果关系的理解 秒懂大纲提出背景解法拆解创意视角中文意译 论文&#xff1a;Knowledge Graph Structure as Prompt: Improving Small Language Models Capabilities for Knowledge-based Ca…

Mybatis框架映射---代码实现(XML配置以及注解形式)

目录 一. 映射关系 1 对 1-映射方式 1.通过xml文件实现映射的一对一关系 总结 &#xff1a; 2.通过注解的方式来实现下面的 1 对 1 的映射关系&#xff0c;实现级联查询 总结&#xff1a; 二. 映射关系多对一 1.通过xml文件实现映射的多对一关系 2.通过注解的方式来实现…

PHP发邮件教程:配置SMTP服务器发送邮件?

PHP发邮件的几种方式&#xff1f;如何使用PHP通过SMTP协议发信&#xff1f; PHP作为一种广泛使用的服务器端脚本语言&#xff0c;提供了多种方式来发送邮件。AokSend将详细介绍如何通过配置SMTP服务器来实现PHP发邮件教程的核心内容。 PHP发邮件教程&#xff1a;设置参数 这…

Qt 模型视图(一):概述

文章目录 Qt 模型视图(一):概述1、模型/视图结构基本原理2、模型3、视图4、代理5、简单实例 Qt 模型视图(一):概述 ​ 模型/视图结构是一种将数据存储和界面展示分离的编程方法。模型存储数据&#xff0c;视图组件显示模型中的数据&#xff0c;在视图组件里修改的数据会被自动…

PCIe扫盲(11)

系列文章目录 PCIe扫盲&#xff08;一&#xff09; PCIe扫盲&#xff08;二&#xff09; PCIe扫盲&#xff08;三&#xff09; PCIe扫盲&#xff08;四&#xff09; PCIe扫盲&#xff08;五&#xff09; PCIe扫盲&#xff08;六&#xff09; PCIe扫盲&#xff08;七&#xff09…

剪画:一带一路机遇下,自媒体如何跨越语言障碍!

随着国家 “一带一路” 的持续推进&#xff0c;我国的文化魅力如璀璨星辰&#xff0c;在世界舞台上熠熠生辉。美食的独特风味、华服的精美绝伦&#xff0c;越来越受到外国人的喜爱。这对于做自媒体的小伙伴们而言&#xff0c;无疑是一个巨大的机遇。 然而&#xff0c;机遇往往与…

利用Leaflet.js和turf.js创建交互式地图:航道路线绘制

引言 在现代Web应用中&#xff0c;地图的交互性是提供丰富用户体验的关键。Leaflet.js是一个轻量级的开源JavaScript库&#xff0c;它提供了简单易用的API来构建交云的地图。与此同时&#xff0c;turf.js作为一个强大的地理空间分析库&#xff0c;能够处理复杂的地理数据操作。…

Broadcast:Android中实现组件及进程间通信

目录 一&#xff0c;Broadcast和BroadcastReceiver 1&#xff0c;简介 2&#xff0c;广播使用 二&#xff0c;静态注册和动态注册 三&#xff0c;无序广播和有序广播 1&#xff0c;有序广播的使用 2&#xff0c;有序广播的截断 3&#xff0c;有序广播的信息传递 四&am…

AI产品经理面试100问,三天看完一周拿4个offer

AI产品经理面试100问 Attention(重点掌握) 1.什么是 Attention?为什么要用 Attention?它有什么作用? 2.Attention的流程是什么样的? 3.普通的Attention和Transformer的Self-attention之间有什么关系? 4.什么是Self-attention? Transformer(重点掌握) 1.Transformer是什…

[Linux]:信号(上)

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ &#x1f388;&#x1f388;养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; 所属专栏&#xff1a;Linux学习 贝蒂的主页&#xff1a;Betty’s blog 1. 信号的引入 1.1 信号的概念 在Linux系统中&#xff0c;信号&#xff08;…

在泰国旅游不会口语怎么办?求推荐翻译软件!!!

如果在泰国旅游时遇到语言障碍&#xff0c;可以采取以下措施&#xff1a;学习一些基础的泰语短语&#xff0c;使用翻译应用程序&#xff0c;携带翻译卡片&#xff0c;利用身体语言&#xff0c;参加有导游的旅行团&#xff0c;选择提供中文服务的酒店和旅行社&#xff0c;使用地…

【leetcode】字典 哈希表习题

1.两数之和&#xff08;查找表法-哈希表&#xff09; 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案&#xff0c;并且你不能使用两次…

【人工智能学习笔记】7_智能语音技术基础

智能语音技术概述 智能语音技术通过对语音进行分析、理解和合成,是计算机设备实现“能听会说”、具备自然语音交流的技术能力。其涉及的范围主要有: 语音合成技术语音识别技术语音测评技术语音降噪与增强技术…智能语音技术的研究内容 智能语音技术的研究难点 智能语音技术…

旅行社区应该如何规划?

近年来&#xff0c;旅游行业逐渐恢复&#xff0c;包括微度假、精致露营、康养旅游、乡村民宿等旅游模式。用户旅游支出、旅游人次逐渐恢复&#xff0c;旅游收入仍待提升。 那么旅游社区应该如何搭建&#xff0c;内容如何规划呢&#xff1f; 我们了解到&#xff0c;很多旅游网…

哪个快?用300万个图斑测试ArcGIS Pro的成对叠加与经典叠加

​​​ 点击下方全系列课程学习 点击学习—>ArcGIS全系列实战视频教程——9个单一课程组合系列直播回放 点击学习——>遥感影像综合处理4大遥感软件ArcGISENVIErdaseCognition 在使用ArcGIS Pro的过程中&#xff0c;很多朋友发现&#xff0c;Pro有个成对叠加工具集。很多…

PSG3D V2024 注册机 蓝天大数据三维测绘系统

PSG3D2024三维测绘系统是基于 AutoCAD 平台开发的&#xff0c;以“智能、 专业、高效、易学”为设计目标开发的全新一代地理数据采集、编辑 和联动的专业软件。 系统集成了三维测图、坐标转换、地形处理、数字地模、无人机 辅助、不动产调查、土地确权、部件普查和数据转换等专…

数字产业中心:技术赋能产业,如何重塑行业格局!

在数字化浪潮的推动下&#xff0c;数字产业中心正逐步成为推动经济转型升级的重要引擎。这里&#xff0c;技术不仅仅是工具&#xff0c;更是重塑行业格局、引领未来发展的核心力量。 一、技术融合创新&#xff0c;打破传统边界 数字产业中心通过云计算、大数据、人工智能等前沿…

【Python】练习:控制语句(二)第1关

第1关&#xff1a;分支结构基础实训 第一题第二题第三题第四题&#xff08;※&#xff09;第五题&#xff08;※&#xff09;第六题第七题 第一题 #第一题 for temp in [-280, -100, 0, 20, 120, 200]:#请在下面编写代码# ********** Begin ********** #if temp>-273.15:F9/…