Android Jetpack(六)Navigation

A, Navigation Introduction        

        Navigation  is a component Jetpack component library, you can easily manage Fragment , it can be seen as the route for at Fragment of. The main features are: for managing navigation page Jump APP , while the switching Fragment more intuitive , the visual interface switching Fragment show a flowchart.

        Using the Navigation Fragment assembly native support for all the benefits you can get architectural components (such as life cycle and ViewModel), while allowing this component to handle the complexity of FragmentTransaction. Further, Navigation assembly may also transition animation process. It can automatically build correct "up" and "return" behavior , includes full support for deep linking, and provides a help program for navigation-related to the appropriate UI widgets, such as drawer and bottom navigation.

        In addition, one of the biggest benefits of Navigation is: Let Activity single application architecture of choice (ie: Single Activity + multi Fragment ) . Fragment jump page within the application by Navigation to deal with, developers do not need the complexity and the related transition of animated FragmentTransaction.

 

Two, Navigation core concepts

  • NavGraph: map , an XML resource that contains all navigation-related information together in one location. This includes all the individual content area (destination), and a user can access the possible paths through the application. Similar to a flowchart following FIG.
  • NavHost: displaying a map in the Destination container . The navigation pack includes a default NavHost achieve NavHostFragment.
  • NavController: control switching of the contents of the container NavHost controller .
  • Destination: fragment generally Fragment (google recommended because the APP single mode Activity), may be Activity. The following three interfaces of FIG.
  • Action: operation equivalent Intent, expressed from a  Destination  to another  Destination , following the arrow in FIG.

        

         

 

Three, Navigation use

1. Import dependencies

implementation 'androidx.navigation:navigation-fragment:2.2.1'
implementation 'androidx.navigation:navigation-ui:2.2.1'

2. Create an Activity and multiple Fragment

        According to the business logic creation, suppose that you create MainActivity, FragmentA, FragmentB.

3.  New Navigation

  • In the rescatalog Right New-> New Resource File, the pop-up New Resource Filedialog box
  • Fill File Name such as: nav_graph, Resource type selection Navigation, click OK

4. Use Navigation

        Open nav_graph.xml, select the Design tab at the bottom, click on the New Destination(top-left corner + ) button, select the pop in
fragment_a.xml,fragment_b.xml;

        Or select Create blank destinationDisplay the following interface after New Fragment:

        

5. Add NavHostFragment

        Add NavHostFragment in Activity in the layout and NavHostFragment navigation associated with us just created.

If it is added by way of drag, it will pop up a dialog box that contains a list of the navigation map, you can select the navigation created; you can also use the code specified

app:navGraph="@navigation/nav_graph"

         

6.  Add Action 

        Hold down the middle of the left circle to the right and then drag the fragment to fragment to navigate then let go.

        

7. The NavController process jumps Action

        Jump need NavController object  navigate(int id)method to jump.

        NavController object obtained in three ways

  • NavHostFragment.findNavController(Fragment)
  • Navigation.findNavController(Activity, @IdRes int viewId)
  • Navigation.findNavController(View)
   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Navigation.findNavController(view)
        button.setOnClickListener {
                    //id就是nav_graph导航图里面的action id
            Navigation.findNavController(view).navigate(R.id.action_blankFragment_to_secondeFragment)
        }
    }

        If it is returned, call  navigateUp()the method.

 

Four, Navigation expand

1. Navigation parameter passing

(1) Code parameter passing

        You can use Bundle of mass participation:

 Bundle bundle = new Bundle();
 bundle.putString("param", "I AM FROM FRAGMENT-A");
 Navigation.findNavController(it).navigate(R.id.action_fragmentA_to_fragmentC, bundle);

 (2) map parameter passing

        A biography as a reference to B, the navigation map design page, click the destination B, add Argument in the property bar to the right of the pop-up window as follows:

        

        Once created, fragment tag Add tag argument as follows:

<argument
    android:name="name"
    android:defaultValue="no"
    app:argType="string" />

        之后可通过两种方式传递参数:

  • FragmentBArgs
  • FragmentADirections

        它们都是自动生成的,FragmentBArgs 是根据fragment 节点下的 argument 节点生成的;FragmentADirections 是根据 action 生成的。

        使用生成的对应的 Agrs 或者 Directions 来传递参数,需要导入 apply plugin: 'androidx.navigation.safeargs'。

//使用FragmentBArgs
val bundle = FragmentBArgs.Builder().setText("Hello World").build().toBundle()
Navigation.findNavController(it).navigate(R.id.action_fragmentA_to_fragmentB, bundle)

//使用FragmentADirections
val direction = FragmentADirections.action_fragmentA_to_fragmentB().setText("Hello World")
Navigation.findNavController(it).navigate(direction)

2. Navigation 动画

        点击箭头,右边属性栏有个Animations列表。默认都是null,可以设置官方自带的进出栈动画,也可以设置自定义动画。

        

 

  • Enter 进入一个目的地 (例如 A跳B, B显示时执行的动画)
  • Exit 退出一个目的地 (例如 A跳B,A隐藏时执行的动画)
  • Pop Enter通过 pop 操作进入目的地 (例如 A跳B,B navigateUp 后 A再次出现时执行的动画)
  • Pop Exit 通过pop 操作退出目的地 (例如 A跳B,B navigateUp 后 B消失时执行的是这个动画)

 

发布了65 篇原创文章 · 获赞 66 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_34519487/article/details/104340094