This is the 8th day of my participation in Gwen Challenge

Follow the public number anan Android to learn more knowledge

Making address:

Github branch: navigation_operation

Code entry

Navigation stack operation

Navigate out of the stack operation

Both methods in this section are used to push the stack navigation component, but the popBackStack approach is more subtle

1, findNavController (.) navigateUp ()

This is a simple way to push the navigation component off the stack. It simply pushes the component at the top of the stack. The Boolean return value of this method indicates whether the stack was successfully pushed. If navigation fails, we can proactively remedy the component’s removal from the stack logically

Code sample
Val navigarionResult = findNavController(). NavigateUp () println(" $navigarionResult")Copy the code

2, findNavController (). PopBackStack (da fragments, R.i false)

This method is used to perform operations that return to the specified page, such as in this example: when entering, we click the AFragment button to jump to the BFragment and execute the following code in the BFragment:

Val popBackStack = findNavController().popbackStack (R.I.D.A Fragment,false) println(" return $popBackStack")Copy the code

Can specify return AFragment.

The second parameter to popBackStack is:

True: returns A from B, pushes A off the stack, and then opens A again

False: When A is returned from B, A will not be removed from the stack and remains in the state before the jump to B

PopUpTo and popUpToInclusive

Take a look at our navigation diagram code

<? The 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/navigation_popup" app:startDestination="@id/popUpFragment"> <fragment android:id="@+id/popUpFragment" android:name="com.mage.navigationdemo.pop.PopUpFragment" android:label="fragment_pop_up" tools:layout="@layout/fragment_pop_up" > <action android:id="@+id/action_popUpFragment_to_popAFragment" app:destination="@id/popAFragment" /> </fragment> <fragment android:id="@+id/popAFragment" android:name="com.mage.navigationdemo.pop.PopAFragment" android:label="fragment_pop_a" tools:layout="@layout/fragment_pop_a" > <action android:id="@+id/action_popAFragment_to_popBFragment" app:destination="@id/popBFragment" /> </fragment> <fragment android:id="@+id/popBFragment" android:name="com.mage.navigationdemo.pop.PopBFragment" android:label="fragment_pop_b" tools:layout="@layout/fragment_pop_b" > <action android:id="@+id/action_popBFragment_to_popUpFragment" app:popUpToInclusive="true" app:popUpTo="@id/popUpFragment" app:destination="@id/popUpFragment" /> </fragment> </navigation>Copy the code

The code in this navigation diagram does the following: Jump from PopUpfragment to PopUpAFragment to PopUpBFragment. Normally, if you continue this way, the PopUpBFragment will jump indefinitely, generating countless Fragment instances.

But in our code, popUpTo and popUpToInclusive are added to the Activion of PopBFragment jump PopUpfragment.

PopUpTo = PopUpfragment = PopUpfragment; PopUpfragment = PopUpfragment; PopAFragment = PopUpfragment; PopUpfragment = PopUpfragment; PopUpfragment = PopUpfragment; PopAFragment = PopUpfragment; PopUpfragment = PopUpfragment; What popUpToInclusive does is take the PopUpFragment off the stack and create it again.

An error is interrupted

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mage.navigationdemo/com.mage.navigationdemo.PopUpToTestActivity}: java.lang.IllegalStateException: Activity com.mage.navigationdemo.PopUpToTestActivity@a289b29 does not have a NavController set on 2131230963
Copy the code

The scenario with this error reappears:

MainActivity is a NavHostFragment. We jump to PopUpToTestActivity using startActivity in the Fragment of MainActivity. Call the following code in the onCreate method of PopUpToTestActivity

 findNavController(R.id.nav_popuptotest).navigate(R.id.popUpFragment)
Copy the code

Jump to PopUpFragment and report the error.

The solution is as follows:

val fragment:NavHostFragment = supportFragmentManager.findFragmentById(R.id.nav_popuptotest) as NavHostFragment
fragment.navController.navigate(R.id.popUpFragment)
Copy the code

That is, find the NavHostFragment of the component through supportFragmentManager and use the NavController of the NavHostFragment to redirect the route

!!!!!!!!! FindNavController ().popbackStack (R.ida Fragment,true

The code is as follows:

  1. Jump from A to B
content.findViewById<Button>(R.id.btn_toBFragment).setOnClickListener {
            findNavController().navigate(R.id.action_AFragment_to_BFragment)
        }
Copy the code
  1. Return A from B using popBackStack (R.ida Fragment,true)
Val popBackStack = findNavController().popbackStack (r.id.a Fragment,true) println(" return $popBackStack")Copy the code
  1. Then jump from A to B using the navigation component
content.findViewById<Button>(R.id.btn_toBFragment).setOnClickListener {
            findNavController().navigate(R.id.action_AFragment_to_BFragment)
        }
Copy the code

The third code will report an error:

 java.lang.IllegalArgumentException: Navigation action/destination com.mage.navigationdemo:id/action_AFragment_to_BFragment cannot be found from the current destination NavGraph(com.mage.navigationdemo:id/navigation) startDestination={Destination(com.mage.navigationdemo:id/AFragment) label=fragment_a class=com.mage.navigationdemo.AFragment}
Copy the code

The reason for the error is that the navigation chart cannot be found,

Cause analysis: When we return to AFragment, AFragment is re-created, which is no longer the root view. That is to say, the new AFragment is not associated with our navigation chart file, so it is naturally impossible to jump to BFragment using the navigation component.

There should be a solution to this situation, so we’re not going in that direction right now

Navigation animation

When using Navigation we can add animations to the Navigation view to achieve a jump animation transition effect.

Ways to add transitions:

Template operation

We can manually add the system animation template in the Navigation panel

Code effect in this example

In this example, we create two fragments, namely AFragment and BFragment. Then we jump from AFragment to BFragment and add animation in the middle of the action of this jump

Effect: