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

一、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.mfbz.cn/a/879815.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Datawhale------Tiny-universe学习笔记——Qwen(1)

1. Qwen整体介绍 对于一个完全没接触过大模型的小白来说&#xff0c;猛一听这个名字首先会一懵&#xff1a;Qwen是啥。这里首先解答一下这个问题。下面是官网给出介绍&#xff1a;Qwen是阿里巴巴集团Qwen团队研发的大语言模型和大型多模态模型系列。其实随着大模型领域的发展&a…

全同台加密综述

文章目录 一、FHE的定义与性质1、核心算法2、性质 二、构造思想三、全同态加密研究进展1、支持部分同态的 Pre-FHE 方案2、基于理想格的 第1代 FHE方案3、基于LWE的 第2代 FHE方案3、基于近似特征向量的 第3代 FHE方案4、支持浮点数运算的 第4代 FHE方案5、其他 FHE方案5.1、基…

数字化时代,住宅代理是怎样为企业赋能的?

在数字化时代&#xff0c;企业的发展也面临着转型&#xff0c;一方面是未知的挑战&#xff0c;一方面是不可多得的机遇。如何在全球市场中保持竞争力是企业要认真思考的问题。如果说主动寻找出路太过冒险&#xff0c;那不妨试试内省式的自我管理革新。代理服务器是一种中介服务…

TI DSP下载器XDS100 V2.0无法使用问题

前言 TI DSP下载器XDS100 V2.0用着用着会突然报Error&#xff0c;特别是你想要用Code Composer Studio烧录下载程序的时候 查看设备管理器&#xff0c;发现XDS100 V2.0的设备端口莫名其妙消失了 问了淘宝的厂家&#xff0c;他说TI的开发板信号可能会导致调试器通信信号中断&a…

软件安全最佳实践:首先关注的地方

尽管组织拥有大量可用的工具&#xff0c;但应用程序安全性仍然不足。 最近的数据显示&#xff0c;在过去四到五年中&#xff0c;软件供应链攻击同比增长了 600-700%&#xff0c;超过一半的美国企业在过去 12 个月中遭受过某种形式的软件供应链攻击。 为何应用程序安全工作未…

相亲交易系统源码详解与开发指南

随着互联网技术的发展&#xff0c;越来越多的传统行业开始寻求线上转型&#xff0c;其中就包括婚恋服务。传统的相亲方式已经不能满足现代人快节奏的生活需求&#xff0c;因此&#xff0c;开发一款基于Web的相亲交易系统显得尤为重要开发者h17711347205。本文将详细介绍如何使用…

WEB攻防-JavaWweb项目JWT身份攻击组件安全访问控制

知识点&#xff1a; 1、JavaWeb常见安全及代码逻辑&#xff1b; 2、目录遍历&身份验证&逻辑&JWT&#xff1b; 3、访问控制&安全组件&越权&三方组件&#xff1b; 演示案例&#xff1a; JavaWeb-WebGoat8靶场搭建使用 安全问题-目录遍历&身份认…

Ubuntu20.04 搜索不到任何蓝牙设备

电脑信息 联想扬天YangTianT4900k 问题描述 打开蓝牙之后&#xff0c;一直转圈&#xff0c;搜索不到任何蓝牙设备 排查 dmesg | grep -i blue 有如下错误&#xff1a; Bluetooth: hci0: RTL: unknown IC info, lmp subver 8852, hci rev 000b, hci ver 000b lsusb 芯片型号如…

imo云办公室 Imo_DownLoadUI.php 任意文件下载漏洞复现

0x01 漏洞描述&#xff1a; imo云办公室由上海易睦网络科技有限公司于2007年创立&#xff0c;总部位于上海&#xff0c;imo云办公室管理运营企业即时通讯平台imo&#xff0c;包括对imo的在线支持&#xff0c;故障处理&#xff0c;客户服务等&#xff0c;对imo进行持续研发&…

Nexpose 6.6.269 发布下载,新增功能概览

Nexpose 6.6.269 for Linux & Windows - 漏洞扫描 Rapid7 Vulnerability Management, release Sep 11, 2024 请访问原文链接&#xff1a;https://sysin.org/blog/nexpose-6/&#xff0c;查看最新版。原创作品&#xff0c;转载请保留出处。 作者主页&#xff1a;sysin.or…

web - JavaScript

JavaScript 1&#xff0c;JavaScript简介 JavaScript 是一门跨平台、面向对象的脚本语言&#xff0c;而Java语言也是跨平台的、面向对象的语言&#xff0c;只不过Java是编译语言&#xff0c;是需要编译成字节码文件才能运行的&#xff1b;JavaScript是脚本语言&#xff0c;不…

Mac 上哪个剪切板增强工具比较好用? 好用剪切板工具推荐

在日常文字编辑中&#xff0c;我们经常需要重复使用复制的内容。然而&#xff0c;新内容一旦复制&#xff0c;旧内容就会被覆盖。因此&#xff0c;选择一款易用高效的剪贴板工具成为了许多人的需求。本文整理了一些适用于 macOS 系统的优秀剪贴板增强工具&#xff0c;欢迎大家下…

人工智能-大语言模型-微调技术-LoRA及背后原理简介

1. 《LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS》 LORA: 大型语言模型的低秩适应 摘要&#xff1a; 随着大规模预训练模型的发展&#xff0c;全参数微调变得越来越不可行。本文提出了一种名为LoRA&#xff08;低秩适应&#xff09;的方法&#xff0c;通过在Transf…

用JS给官方电子课本扩展个下载功能

为了方便学生、老师和家长&#xff0c;官方提供了几乎所有在用的正版电子课本&#xff0c;由于没有下载功能&#xff0c;只能在线看&#xff0c;有点不方便。 为了更方便使用&#xff0c;用JS外挂了一个下载按钮。 扩展后效果如图&#xff1a; &#xff08;根据2022年版课程…

fastadmin 部署后前台会员中心出现404错误

访问前台会员中心出现404错误。 解决&#xff1a;nginx访问站点增加伪静态 location / {if (!-e $request_filename){rewrite ^(.*)$ /index.php?s$1 last; break;} }在phpstydy中增加伪静态&#xff0c;如图&#xff1a;

基于java的工费医疗报销管理系统设计与实现

博主介绍&#xff1a;专注于Java vue .net php phython 小程序 等诸多技术领域和毕业项目实战、企业信息化系统建设&#xff0c;从业十五余年开发设计教学工作 ☆☆☆ 精彩专栏推荐订阅☆☆☆☆☆不然下次找不到哟 我的博客空间发布了1000毕设题目 方便大家学习使用 感兴趣的…

专题六_模拟_算法详细总结

目录 模拟算法 1.模拟算法流程&#xff08;一定要在草稿纸上演算一遍流程&#xff09; 2.把流程转换成代码 1. 替换所有的问号&#xff08;easy&#xff09; 解析&#xff1a; 1.暴力&#xff1a; 2.优化&#xff1a;&#xff08;找规律&#xff09; 总结&#xff1a; …

Vue3+Element Plus:使用el-dialog,对话框可拖动,且对话框弹出时仍然能够在背景页(对话框外部的页面部分)上进行滚动以及输入框输入信息

【需求】 使用Element Plus中的el-dialog默认是模态的&#xff08;即它会阻止用户与对话框外部的元素进行交互&#xff09;&#xff0c;对话框弹出时仍然能够在背景页&#xff08;对话框外部的页面部分&#xff09;上进行滚动以及输入框输入信息&#xff0c;且对话框可拖动 【…

TCP/IP五层模型

OSI七层模型 OSI(Open Systems Interconnection)七层模型是一种概念框架&#xff0c;用于标准化不同计算机系统之间的通信过程 它由国际标准化组织(ISO)在1984年提出&#xff0c;主要用于网络通信 这七层模型从上到下分别是: 应用层(Application Layer):为应用软件提供网络服…

tomcat服务器

tomcat简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器&#xff0c;属于轻量级应用服务器。Tomcat 虽然和 Apache 或者 Nginx 这些 Web 服务器一样&#xff0c;具有处理 HTML 页面的功能&#xff0c;然而由于其处理静态 HTML 的能力远不及 Apache 或者 Nginx&#x…