Android Navigation2 - In-depth understanding of Navigation

foreword

Under some force majeure factors, if an APP can only have one Activity, and other pages are displayed in fragments, it is also very convenient to use navigation for routing at this time. But you have to manage the back stack of all your pages.

Before that, I wrote an article on simple use of navigation, which is relatively superficial. Now I have a deeper impression on navigation. Let me record it

need

In the previous fragment addition and deletion operations, FragmentManager's transaction was used to operate, that is

getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.fragment_container,new YourFragment())
        .addToBackStack(null)
        .commit(); 

Looking through the official navigation document, we can find that in addition to using an <action> id to jump normally, we can also jump directly to the destination, or add some navigation options (navOptions) when jumping

Example: Jump directly to the destination mainPageFragment

NavHostFragment nav = (NavHostFragment)     
    getSupportFragmentManager().findFragmentById(R.id.page_host);
NavController navController = nav.getNavController();
navController.navigate(R.id.mainPageFragment);

But whether we directly specify the destination to jump or use action to jump, the path we jumped to will be added to the back stack.

For example, after the user jumps back to A from the order of ABCA, the user clicks Back and will return to the CBA page in turn. Then I think what should I do if the user does not return to C when I am in CA?

Solution

In our nav.xml file, add popUpTo to CA's <action> and set popUpToInclusive to true 

<?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/main_nav"
    app:startDestination="@id/fragmentA">

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.metajoy.mynavigationdemo2.FragmentA"
        android:label="FragmentA" >
        <action
            android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB" />
    </fragment>
    <fragment
        android:id="@+id/fragmentB"
        android:name="com.metajoy.mynavigationdemo2.FragmentB"
        android:label="FragmentB" >
        <action
            android:id="@+id/action_fragmentB_to_fragmentC"
            app:destination="@id/fragmentC" />
    </fragment>
    <fragment
        android:id="@+id/fragmentC"
        android:name="com.metajoy.mynavigationdemo2.FragmentC"
        android:label="FragmentC" >
        <action
            android:id="@+id/action_fragmentC_to_fragmentA"
            app:destination="@id/fragmentA"
            app:popUpTo="@id/fragmentA"
            app:popUpToInclusive="true"
            />
    </fragment>
</navigation>

 Code way:

NavOptions navOptions = new NavOptions.Builder()
                .setPopUpTo(R.id.fragmentA,true)
                        .build();
        view.findViewById(R.id.c_bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navTo(R.id.action_fragmentC_to_fragmentA,navOptions);
                //navTo(R.id.action_fragmentC_to_fragmentA);
            }
        });

In this way, ABCA can be realized, and A clicks the return button to directly exit the APP operation

Guess you like

Origin blog.csdn.net/TDSSS/article/details/128659908