This is the 10th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

In the previous article, we introduced the three main concepts of Navigation: NavHost, NavGraph, and NavController. We can use these concepts to quickly implement the following functions:

  • Correct navigation and rollback
  • Add navigation animation
  • Modular partial navigation
  • Convenient page reuse
  • Jumps to the specified page from outside the application

Do not know the above function has solved the pain point that you develop, if have, look to go down.

As you can see from the visual navigation in the previous article, NavGraph looks like a directed graph, and any path you can take in a project is specified in advance, which is a guarantee for project development. In fact, NavGraph visualization of XML is an implementation of visualization of its data structure, and all attributes in XML can be implemented in code, which you should know in advance.

jump

The jump is very simple, the NavController performs a navigation path, and when you are the starting point of the navigation, it replaces the page with the end point.

nav_graph.xml

<fragment
        android:id="@+id/test1Fragment"
        android:name="com.example.navigationexample.ui.test.Test1Fragment"
        android:label="Test1Fragment" >
        <action
            android:id="@+id/action_test1Fragment_to_test2Fragment"
            app:destination="@id/test2Fragment" />
    </fragment>
<fragment
        android:id="@+id/test2Fragment"
        android:name="com.example.navigationexample.ui.test.Test2Fragment"
        android:label="Test2Fragment" />
Copy the code

Test.kt

findNavController().navigate(R.id.action_test1Fragment_to_test2Fragment)
Copy the code

The fallback

The relevant functions are as follows:

  • popUpToInclusive()
  • popUpTo()
  • Java/kt files: NavController navigateUp () or NavController. PopBackStack () to remove the page page stack

There are three implementations of fallback, and the following examples illustrate the logic of each implementation.

By default the fallback

Home page Suppose we have multiple fragments :fA, fB, fC, fD.

The nav. XML diagram has a path fA->fB->fC->fD->fB. When we finish this path in the default way, the page stack now has: A,B,C,D,B (bottom to top).

When we use NavController. NavigateUp () or NavController. PopBackStack (after), at this point in the back stack is: A, B, C, D

Fall back to a specific page (without overwriting the target specific page)

A,B,C,D. When we add app:popUpTo to the fD->fB action, this is the code

<action
        android:id="@+id/action_fd_to_fb"
        app:destination="@id/fb"
        app:popUpTo="@+id/fb"/>
Copy the code

After going down this path again, the page stack looks like this:

A,B,B

Fall back to a specific page (override target specific page)

Continuing with the example above: this time the stack is unrolled: A,B,C,D. Let’s add app:popUpToInclusive=”true”

<action
        android:id="@+id/action_fd_to_fb"
        app:destination="@id/fb"
        app:popUpTo="@+id/fb"
        app:popUpToInclusive="true"/>
Copy the code

The page stack looks like this:

Type A, B,

The difference between this method and the previous one is whether the old FragmentB is deleted.

Rollback limits

At this point you might be wondering, what if I want to send back data from fD to fB? What if you want to use an old FragmentB instance?

Sorry, this is not recommended in principle. It is not recommended to use activities and fragments to save data and state, which may cause data loss and state loss. So in JetPack it is always recommended that the ViewModel store data state and so on. There is no need to have an old Fragment instance, it is best practice to use the ViewModel to hold data except when Navigation passes the jump with very little data. This also explains why, by default, retracting from one Fragment to the previous Fragment is replaced instead of show.