Android jetpack-Navigation Operating principle analysis (1)

Navigation frame creation

If we want to create a Project that uses the Navigation framework, we can directly select the Bottom Navigation Activity option in Android Studio’s New Project as shown This way we can create a new project with three bottoms and the Navigation dependencies are already set in build.gradle so we don’t have to manually add them, and it helps us to follow the path to understand how Navigation works.

Working principle of Navigation

After we create the project, when we click the bottom three buttons, we can switch back and forth between fragments. How is the Navigation framework implemented specifically? Let’s start with MainActivity

  • MainActivity
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); }}Copy the code

The main function implemented in the whole MainActivity is pretty obvious, get the BottomNavigationView layout ID, create the AppBarConfiguration as the builder, Finally, create a new controller NavController and configure BottomNavigationView and AppBarConfiguration, so that you can achieve the effect of clicking back and forth

  • R.layout.activity_main

      
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="? attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="? android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

Copy the code

In the Activity_main layout file, you can see that the first part is called the BottomNavigationView and the second part is called the fragment. The first part needs to pay attention to is the app: Menu property, which contains some icon configuration display information, etc. If you want to add, delete and change the bottom icon needs to operate in the inside, the second part needs to pay attention to the name property, defaultNavHost property and navGraph property

Name: represents the real implementation class of the current Fragment tag defaultNavHost: whether it is associated with the system return key. When clicking the return key, check whether Fragement still exists in the rollback stack. If yes, the return key event will be intercepted. NavGraph: Page specific routing structure

  • mobile_navigation

      
<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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="com.gcp.juejintest.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.gcp.juejintest.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.gcp.juejintest.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />

Copy the code

StartDestination: displays the page by default

Add deepLink tags to the Fragment TAB in the navigation frame as follows:

 <fragment
        android:id="@+id/navigation_home"
        android:name="com.gcp.juejintest.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home">
        <deepLink
            android:id="@+id/deepLink"
            app:uri="www.jj.com" />
    </fragment>
Copy the code

If there are browsers and third-party applications that need to pull up the project and be specific to a fragment or similar scenario, then we can use deepLink as a solution to configure deepLink tags in XML and write uri values. You can use the handleDeepLink method directly in your code at call time

        navController.handleDeepLink(getIntent());
Copy the code

This is how to use the Navigation framework in a resource file. In addition to using the Navigation framework in a resource file, you can also use the Navigation framework dynamically through annotations.Android- Jetpack-Navigation Operating principle analysis (2)