Android JetPack –导航架构

In this tutorial, we’ll be discussing the Navigation Architecture that is a part of the JetPack.
JetPack is a set of components and libraries introduced at Google I/O 2018 to build better applications.

在本教程中,我们将讨论作为JetPack一部分的导航架构。
JetPack是Google I / O 2018引入的一组组件和库,用于构建更好的应用程序。

Android Jetpack consists of the following components:

Android Jetpack包含以下组件:

Android JetPack导航架构组件 (Android JetPack Navigation Architecture Component)

The Navigation Architecture component is a part of the new AndroidX package that’s introduced since Android SDK 28.

导航体系结构组件是从Android SDK 28开始引入的新AndroidX程序包的一部分。

This component consists of new guidelines to structure your application, especially navigation between Fragments.

该组件由新准则组成,这些新准则用于构建应用程序,尤其是在片段之间导航。

Google recommends the Single Activity Architecture moving forward when using JetPack.
Navigation Architecture gets ride of the complex FragmentTranscation by providing its own set of classes to navigate between fragments. Let’s first look into the guidelines for Navigation Architecture.

Google建议使用JetPack的单活动架构。
导航体系结构通过提供自己的一组类在片段之间进行导航,从而摆脱了复杂的FragmentTranscation的困扰。 首先让我们研究一下导航体系结构的准则。

导航原理 (Principles of Navigation)

  • The app should have a fixed starting destination.

    该应用程序应具有固定的启动目标。
  • A stack is used to represent the navigation state of an app.

    堆栈用于表示应用程序的导航状态。
  • The Navigation Up function never exits your app.

    向上导航功能永远不会退出您的应用程序。
  • Up and Back should function identical in all the other cases.

    在其他所有情况下,“上”和“后”功能应相同。
  • Deep linking to a destination or navigating to the same destination should yield the same stack.

    深度链接到目标或导航到相同的目标应产生相同的堆栈。

入门 (Getting Started)

Create a new Android Studio Project. Add the following classpath inside the root build.gradle file:

创建一个新的Android Studio项目。 在root build.gradle文件中添加以下类路径:

buildscript {
	...
	repositories {
    		google()
	}
	dependencies {
    		...
    		classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha04'
	}
}

Inside the app’s build.gradle add the following:

在应用程序的build.gradle添加以下内容:

implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha04'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha04'

Add the following plugin inside app’s build.gradle:

在应用程序的build.gradle添加以下插件:

apply plugin: 'androidx.navigation.safeargs'

项目结构 (Project Structure)

Our project consists of a single activity and two fragments. Let’s see how we arrived at this.

我们的项目包含一个活动和两个片段。 让我们看看我们是如何得出的。

导航图 (Navigation Graph)

A Navigation Graph is the core layer of the Navigation Architecture. It lays out all the fragments/activities and adds all the connections between them.

导航图是导航体系结构的核心层。 它列出了所有片段/活动并添加了它们之间的所有连接。

Within this graph, we address different parts using a few key terms:

在此图中,我们使用一些关键术语来处理不同的部分:

Navigation Graph XML – This is an XML defined which is created inside the res folder. Right click on the res directory and choose New -> Android resource file. Set the title for the file and choose Navigation from the Resource type dropdown as shown below:

导航图XML –这是在res文件夹中创建的XML定义。 右键单击res目录,然后选择“新建”->“ Android资源文件”。 设置文件标题,然后从“资源类型”下拉列表中选择“导航”,如下所示:

This creates a navigation folder inside the res with the relevant file name.

这将在res内创建具有相关文件名的导航文件夹。

Navigation Host Fragment
Inside the Activity layout, we define a Navigation Host Fragment. In our project, we’ve set the navigation host fragment inside the activity_main.xml layout as shown below:

导航主机片段
在活动布局内,我们定义了一个导航主机片段。 在我们的项目中,我们在activity_main.xml布局中设置了导航主机片段,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <fragment
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/navigation_graph" />


</androidx.constraintlayout.widget.ConstraintLayout>
  • app:navGraph: defines which Navigation Graph will be associated with the Navigation Host

    app:navGraph :定义将与导航主机关联的导航图
  • app:defaultNavHost="true": ensures that the Navigation Host intercepts the system back button when pressed.

    app:defaultNavHost="true" :确保按下时导航主机拦截系统后退按钮。

Destination – A Destination is any fragment or activity where the user can go.

目标 –目标是用户可以去的任何片段或活动。

We can add destinations inside the Navigation Graph Design file as:

我们可以在“导航图设计”文件中添加以下目标:

  • On the left, we have the Destinations

    在左侧,我们有目的地
  • In the middle, we have the Navigation Graph

    在中间,我们有导航图
  • On the right, we have the Attributes editor. Inside the attributes editor, we can add actions, pass arguments

    在右侧,我们有属性编辑器。 在属性编辑器中,我们可以添加操作,传递参数

Notice the circular icon on the FirstFragment when it is highlighted. These are Actions.

请注意, FirstFragment上的圆形图标突出显示时。 这些是动作。

Actions – These are defined to navigate from one fragment/activity to another. We can define them by dragging or in the XML.

动作 –定义为从一个片段/活动导航到另一个片段/活动。 我们可以通过拖动或在XML中定义它们。

An action is created with an id specified. You can choose the transition animations as well from a dropdown list of built-in ones or create your own and specify it.
The XML code gets auto-generated:

创建具有指定ID的操作。 您也可以从内置动画的下拉列表中选择过渡动画,也可以创建自己的动画并进行指定。
XML代码会自动生成:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.journaldev.androidjetpacknavigation.FirstFragment"
        android:label="navigation_first_fragment"
        tools:layout="@layout/navigation_first_fragment" >
        <action
            android:id="@+id/action_firstFragment_to_secondFragment"
            app:destination="@id/secondFragment"
            app:enterAnim="@anim/nav_default_enter_anim"  />
    </fragment>
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.journaldev.androidjetpacknavigation.SecondFragment"
        android:label="navigation_second_fragment"
        tools:layout="@layout/navigation_second_fragment" />
</navigation>

The above code can be written directly without the need of the Design Editor.
We’ve added an animation on the action.

无需设计编辑器即可直接编写以上代码。
我们在动作上添加了动画。

Inside the Navigation Graph XML tag, you must specify the startDestination. The application gets launched there.

在Navigation Graph XML标记内,必须指定startDestination 。 该应用程序在那里启动。

The layouts for the First and Second Fragment are:

第一个和第二个片段的布局为:

navigation_first_fragment.xml

navigation_first_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".FirstFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:gravity="center"
        android:textSize="22sp"
        android:textColor="@android:color/white"
        android:text="This is First Fragment" />

    <Button
        android:id="@+id/button_frag1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Go to next screen" />
</LinearLayout>

navigation_second_fragment.xml

navigation_second_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:gravity="center"
    tools:context=".SecondFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="This is Second Fragment"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

    <Button
        android:id="@+id/button_frag2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Back" />

</RelativeLayout>

Now that the layouts are ready, let’s see how to navigate from one Fragment to the other via the actions.
NavController manages app navigation within the NavHost.

现在已经准备好布局,让我们看看如何通过操作从一个Fragment导航到另一个Fragment。
NavController管理中的应用程序导航NavHost

The code for the FirstFragment.java class is:

FirstFragment.java类的代码为:

package com.journaldev.androidjetpacknavigation;

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

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

public class FirstFragment extends Fragment {

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


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


        View.OnClickListener s = Navigation.createNavigateOnClickListener(R.id.action_firstFragment_to_secondFragment);
        Button button = view.findViewById(R.id.button_frag1);
        button.setOnClickListener(s);
    }

}

We pass the action id inside the createNavigateOnClickListener method. There are a few alternative ways to navigate as well:

我们在createNavigateOnClickListener方法内部传递操作ID。 还有几种其他导航方式:

Alternative Way 1
We can navigate using the following code as well:

替代方法1
我们也可以使用以下代码进行导航:

final NavController navController = Navigation.findNavController(getActivity(), R.id.my_nav_host_fragment);

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navController.navigate(R.id.action_firstFragment_to_secondFragment);
            }
        });

Alternative Way 2

替代方法2

final NavDirections navDirections = FirstFragmentDirections.actionFirstFragmentToSecondFragment();
final NavController navController = Navigation.findNavController(getActivity(), R.id.my_nav_host_fragment);

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navController.navigate(navDirections);
            }
        });

传递参数 (Passing Arguments)

The navigation architecture comes with a built-in way to pass type-safe arguments from one Fragment to the other.

导航体系结构内置了一种将类型安全参数从一个Fragment传递到另一个Fragment的内置方法。

You can define them in the Navigation Graph design editor as:

您可以在“导航图”设计编辑器中将它们定义为:

and/or in XML:

和/或XML:

<fragment
        android:id="@+id/firstFragment"
        android:name="com.journaldev.androidjetpacknavigation.FirstFragment"
        android:label="navigation_first_fragment"
        tools:layout="@layout/navigation_first_fragment" >

        <argument
            android:name="test_string"
            android:defaultValue="hello world"
            app:argType="string" />
    </fragment>

app:argType should be used in place of app:type. app:type is deprecated.

app:argType应该代替app:type 。 app:type已弃用。

Now when we navigate these args get automatically passed.
We can also pass more arguments programmatically.

现在,当我们导航时,这些参数会自动传递。
我们还可以通过编程方式传递更多参数。

Our onViewCreated method of the FirstFragment now becomes:

现在,我们的FirstFragment的onViewCreated方法变为:

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


        final Bundle bundle = new Bundle();
        bundle.putBoolean("test_boolean", true);

        final NavController navController = Navigation.findNavController(getActivity(), R.id.my_nav_host_fragment);

        
        Button button = view.findViewById(R.id.button_frag1);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                navController.navigate(R.id.action_firstFragment_to_secondFragment, bundle);
            }
        });
    }

The code for the SecondFragment.java class is:

SecondFragment.java类的代码是:

package com.journaldev.androidjetpacknavigation;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

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

public class SecondFragment extends Fragment {

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        Toast.makeText(getActivity().getApplicationContext(), "Bundle args " + getArguments().getBoolean("test_boolean"), Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity().getApplicationContext(), "Bundle args " + FirstFragmentArgs.fromBundle(getArguments()).getTestString(), Toast.LENGTH_SHORT).show();

        Button button = view.findViewById(R.id.button_frag2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final NavController navController = Navigation.findNavController(getActivity(), R.id.my_nav_host_fragment);
                navController.navigateUp();

                navController.addOnNavigatedListener(new NavController.OnNavigatedListener() {
                    @Override
                    public void onNavigated(@NonNull NavController controller, @NonNull NavDestination destination) {
                        Log.d("TAG", destination.getLabel() + " ");
                    }
                });
            }
        });
    }
    
}

The arguments passed in the Navigation Graph have auto-generated getters when you re-build the project.

重建项目时,“导航图”中传递的参数具有自动生成的吸气剂。

FirstFragmentArgs.fromBundle(getArguments()).getTestString() is used to retrieve the argument passed from the FirstFragment.
navigateUp() is used to return to the previous fragment.
addOnNavigatedListener is called when the navigation is completed.

FirstFragmentArgs.fromBundle(getArguments()).getTestString()用于检索从FirstFragment传递的参数。
navigateUp()用于返回上一个片段。
导航完成后,将调用addOnNavigatedListener

The output of the application in action is:

实际应用程序的输出为:

This brings an end to this tutorial. You can download the project from the link below:

本教程到此结束。 您可以从下面的链接下载项目:

翻译自: https://www.journaldev.com/22299/android-jetpack-navigation-architecture

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值