添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

The Navigation Architecture Component simplifies implementing navigation, while also helping you visualize your app's navigation flow. The library provides a number of benefits, including:

  • Automatic handling of fragment transactions
  • Correctly handling up and back by default
  • Default behaviors for animations and transitions
  • Deep linking as a first class operation
  • Implementing navigation UI patterns (like navigation drawers and bottom nav) with little additional work
  • Type safety when passing information while navigating
  • Android Studio tooling for visualizing and editing the navigation flow of an app
  • What you'll build

    In this codelab, you will work with the sample app seen below (you may need to right click this image and choose "Open image in new tab" otherwise it may be too large to view):

    All the activities and fragments have already been created for you. You will use the Navigation Component to connect them and in doing so, implement the following:

  • Visual navigation graph
  • Navigation by destination and action
  • Transition animations
  • Menu navigation, bottom navigation, and menu drawer navigation
  • Type safe argument passing
  • Deep links
  • Prerequisites

  • Basic Java knowledge (this codelab is in Java)
  • Android Studio 3.2 or higher
  • Emulator or device running API 14+
  • Get the Code
    You should download the project as a Zip file from Blackboard.

    Get Android Studio 3.3 or higher

    Make sure you are using Android Studio 3.3 or higher. This is required for the Android Studio navigation tooling.

    If you need to download a recent version of Android Studio, you can do so here .

    Overview of Navigation

    The Navigation Component consists of three key parts, working together in harmony. They are:

  • Navigation Graph (New XML resource) - This is a resource that contains all navigation-related information in one centralized location. This includes all the places in your app, known as destinations, and possible paths a user could take through your app.
  • NavHostFragment (Layout XML view) - This is a special widget you add to your layout. It displays different destinations from your Navigation Graph.
  • NavController (Kotlin/Java object) - This is an object that keeps track of the current position within the navigation graph. It orchestrates swapping destination content in the NavHostFragment as you move through a navigation graph.
    When you navigate, you'll use the NavController object, telling it where you want to go or what path you want to take in your Navigation Graph. The NavController will then show the appropriate destination in the NavHostFragment .
  • That's the basic idea. Let's see what this looks like in practice, starting with the new Navigation Graph resource.

    Destinations

    The Navigation Component introduces the concept of a destination . A destination is any place you can navigate to in your app, usually a fragment or an activity. These are supported out of the box, but you can also make your own custom destination types if needed.

    Navigation Graph

    A navigation graph is a new resource type that defines all the possible paths a user can take through an app. It shows visually all the destinations that can be reached from a given destination. Android Studio displays the graph in its Navigation Editor. Here's part of the starting navigation graph you'll create for your app:
    Nav Graph

    Exploring the Navigation Editor

  • Open res/navigation/mobile_navigation.xml
  • Click the Design tab at the top right hand corner to go into Design mode. You should see the following:
    Navigation design
    The navigation graph shows the available destinations. The arrows between the destinations are called actions. You'll learn more about actions later.
  • Click on a destination to see its attributes.
    Destination Step 2
  • Click on any action, represented by an arrow, to see its attributes.
    Action Selected
  • Anatomy of a navigation XML file

    All of the changes you make in the graphical Navigation Editor change the underlying XML file, similar to the way the Layout Editor modifies the layout XML.

    Click the Code tab and you will see the XML source of the file:
    Nav Graph Code

    Notice:

  • <navigation> is the root node of every navigation graph.
  • <navigation> contains one or more destinations, represented by <activity> or <fragment> elements.
  • app:startDestination is an attribute that specifies the destination that is launched by default when the user first opens the app.
  • Let's take a look at a fragment destination:

        <fragment
    		android:id="@+id/flow_step_one_dest"
    		android:name="uk.aston.androidnavigation.FlowStepFragment"
    		tools:layout="@layout/flow_step_one_fragment">
            <argument
            <action
                android:id="@+id/next_action"
                app:destination="@id/flow_step_two_dest"></action>
        </fragment>
    

    Notice:

  • android:iddefines an ID for the fragment that you can use to reference the destination elsewhere in this XML and your code.
  • android:name declares the fully qualified class name of the fragment to instantiate when you navigate to that destination.
  • tools:layout specifies what layout should be shown in the graphical editor.
    Some <fragment> tags also contain <action>, <argument>, and <deepLink>, all of which we'll cover later.
  • The sample app starts with a few destinations in the graph. In this step, you'll add a brand new destination! You must add a destination to the navigation graph before you can navigate to it.