The best practice of using Navigation UI Kotlin expansion framework in the Java class library
The best practice of using Navigation UI Kotlin expansion framework in the Java class library
Navigation is one of the common functions in mobile applications.In order to simplify navigation mode management in Android applications, Android Jetpack provides Navigation components.This component uses the Navigation UI library to help developers easily process navigation and interface switching.
The Navigation UI library provides some convenient expansion methods to handle navigation -related operations in the Java library.This article will introduce the best practice of how to use the Navigation UI Kotlin expansion framework.
1. Preparation
Before starting, make sure your Android project uses Navigation components.In the build.gradle file, add the following dependencies:
gradle
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
2. Create navhostfragment
First of all, create a Navhostfragment, which is the core part of the Navigation component to accommodate the target destination of all navigation.
In the XML layout file, add an empty Framelayout as a container of Navhostfragment:
<FrameLayout
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
3. Set Navcontroller
Next, in Activity or Fragment, obtain NavController objects of Navhostfragment, and set up to Toolbar or ActionBar's supporting navigation controllers.
In Activity:
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
Toolbar toolbar = findViewById(R.id.toolbar);
NavigationUI.setupWithNavController(toolbar, navController);
In Fragment:
NavHostFragment navHostFragment = (NavHostFragment) getParentFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavController navController = navHostFragment.getNavController();
Toolbar toolbar = requireView().findViewById(R.id.toolbar);
NavigationUI.setupWithNavController(toolbar, navController);
Fourth, define destinations
Navigation components define each interface through destinations.The destination can be Activity, Fragment or other components.
In the Navigation figure, define the ID and associated Fragment class of each destination:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.example.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/profileFragment"
android:name="com.example.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile" />
<!-Add other destinations->
</navigation>
5. Processing navigation operation
Using the expansion method of the Navigation UI library, various navigation operations can be processed, such as the click button, navigation icon, etc.Here are some examples:
1. Use the button for navigation:
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.profileFragment));
2. Return to the previous destination:
@Override
public void onBackPressed() {
if (!Navigation.findNavController(this, R.id.nav_host_fragment).popBackStack()) {
super.onBackPressed();
}
}
3. Add the navigation behavior of the TOOLBAR menu item:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
Toolbar toolbar = findViewById(R.id.toolbar);
NavigationUI.setupWithNavController(toolbar, navController);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
}
6. Add deep -layered link support
The Navigation UI library also supports the use of deep links to navigate to a specific destination in the application.
In the Manifest file, add a INTENT filter to the specific Activity, specify the corresponding URI and destination:
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<nav-graph android:value="@navigation/nav_graph" />
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="example.com"
android:pathPrefix="/profile"
android:scheme="https" />
<data
android:host="example.com"
android:pathPrefix="/home"
android:scheme="https" />
</intent-filter>
</activity>
7. Use safety parameters for type Safe navigation
In order to avoid type errors in the navigation process, safety parameters can be used for type safety navigation.Use parameter interpolation syntax to pass parameters between destinations.
In the navigation diagram, the definition parameters:
<fragment
android:id="@+id/profileFragment"
android:name="com.example.ProfileFragment"
android:label="Profile"
tools:layout="@layout/fragment_profile">
<argument
android:name="user"
app:argType="com.example.User" />
</fragment>
When navigation, pass the parameters:
User user = new User("John", "Doe");
Bundle bundle = new Bundle();
bundle.putParcelable("user", user);
Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.profileFragment, bundle);
Destination of parameters:
ProfileFragmentArgs args = ProfileFragmentArgs.fromBundle(getArguments());
User user = args.getUser();
The above is the best practice of using the Navigation UI Kotlin extension framework in the Java library.This framework provides a simple and powerful way to manage navigation in Android applications.Using these techniques can easily build a stable application navigation system.