Modular Navigation System in Android with Jetpack Navigation
Introduction
In Android application development, managing navigation between screens is crucial to ensuring a smooth and scalable user experience. Jetpack Navigation provides a robust solution for handling navigation, but in large and modular applications, it can become difficult to maintain. In this article, we will explore a Jetpack Navigation-based system designed to be modular and scalable.
System Architecture
The navigation system is organized into independent modules, following the principle of separation of concerns. The architecture is divided into the following modules:
- app: Contains the main activity and the navigation host.
- core:navigation: Defines public destinations and routes accessible from other modules.
- feature:feature1 and feature:feature2: Each module represents a specific functionality and contains its own screens and internal routes.
Module Diagram
The following diagram illustrates the modular structure of the navigation system:
This modular structure allows each functionality to have its own module, facilitating development and maintenance without affecting other parts of the application.
Navigation System Implementation
The system follows a route-based navigation pattern. Each feature module defines its own navigation routes in an encapsulated manner and only exposes those that need to be accessible by other modules via the core:navigation module.
Class Diagram of the Navigation System
Component Description
Below is a detailed description of the classes that form part of the navigation system:
- AppNavHost: This is the central component of the application’s navigation. It defines the NavHostController and manages navigation between modules. It registers all accessible routes in the application and delegates control to each module when necessary.
- MainRoutes: Defines the main routes of the app module.
- Feature1Routes and Feature2Routes: Responsible for defining the available routes within each feature module. These classes encapsulate each module’s internal routes, allowing proper control of the navigation flow.
- PublicDestinations: Contains the application’s public routes that can be accessed by other modules. This approach ensures that each module can access global routes without knowing the internal implementation details of other modules.
- PrivateFeatureDestinations: Defines the private routes of each feature module. These routes are not accessible from outside the module, ensuring proper encapsulation and preventing other modules from depending on specific functionality routes.
- Feature1Screen and Feature2Screen: These are the main screens of each module. These classes define the UI for each feature and receive the necessary arguments from navigation.
Classes with internal Visibility
In this navigation system, some classes and objects are declared with internal visibility. This means they can only be accessed within the same module where they are defined. This approach provides several advantages:
- Encapsulation: Limits access to certain elements to prevent them from being used outside the context of the module where they were designed.
- Modularity: By restricting access, coupling between modules is reduced, allowing each one to evolve independently.
- Code Security: By not exposing certain elements outside the module, the risk of them being used incorrectly in other parts of the application is minimized.
In f_eature1_ and f_eature2_, some classes and objects are marked as internal to limit their access only to the module where they are defined. These include PrivateFeatureDestinations, PrivateFeature1Screen, PrivateFeature2Screen, as well as their respective internal routes.
- PrivateFeature1Screen and PrivateFeature2Screen: These are internal screens within the f_eature1_ and f_eature2_ modules, used to handle specific flows within each module without being exposed outside of them. Since they are internal, they ensure that they can only be accessed within the same module, providing better code organization and encapsulation.
- PrivateFeatureDestinations: Defines the internal routes within each feature module. These routes can only be used within the same module, preventing other parts of the application from depending on their internal structure.
Using internal provides a high level of encapsulation, ensuring that each module can evolve autonomously without creating undesired dependencies with other project modules.
Navigation Implementation with Jetpack Navigation
Each module defines its own routes and destinations as follows:
**Route Definition in **core:navigation
The core:navigation module acts as an intermediary to define globally accessible routes in the application. Here, the main routes that can be used by other modules are defined in the PublicDestinations file.
interface Route
@Serializable
object Main : Route
@Serializable
object Feature1 : Route
@Serializable
object Feature2 : Route
interface Route
@Serializable
object Main : Route
@Serializable
object Feature1 : Route
@Serializable
object Feature2 : Route
**Navigation Implementation in **AppNavHost
The AppNavHost is the central navigation component of the application. It defines a NavHostController, which manages navigation across modules. The navigation starts from a defined startDestination, typically a main screen or entry point of the application.
Within AppNavHost, a NavHost is created using Jetpack Navigation. The NavHost registers different sets of routes corresponding to different feature modules. Each module contributes its own navigation structure through specific route functions.
- mainRoutes(navController): Registers the main navigation routes of the app.
- feature1Routes(navController): Registers navigation routes for feature1.
- feature2Routes(navController): Registers navigation routes for feature2.
By organizing navigation in this way, the system remains modular, allowing individual features to define their own routes independently while still being linked into the global navigation structure.
@Composable
fun AppNavHost(
navController: NavHostController = rememberNavController(),
startDestination: Route = Main,
) {
NavHost(navController = navController,
startDestination = startDestination
) {
mainRoutes(navController = navController)
feature1Routes(navController = navController)
feature2Routes(navController = navController)
}
}
@Composable
fun AppNavHost(
navController: NavHostController = rememberNavController(),
startDestination: Route = Main,
) {
NavHost(navController = navController,
startDestination = startDestination
) {
mainRoutes(navController = navController)
feature1Routes(navController = navController)
feature2Routes(navController = navController)
}
}
Screen Implementation in Each Module
Each module defines its own screens and internal destinations, ensuring encapsulation and modularity by exposing only the necessary routes.
- feature1
The f_eature1_ module defines its navigation structure through F_eature1Routes_, which registers both public and private routes for the module:
<button data-code="fun NavGraphBuilder.feature1Routes(navController: NavController) {
composable {
Feature1Screen(navController = navController)
}
composable {
PrivateFeature1Screen()
}
}” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fun NavGraphBuilder.feature1Routes(navController: NavController) {
composable<Feature1> {
Feature1Screen(navController = navController)
}
composable<PrivateFeature1> {
PrivateFeature1Screen()
}
}
fun NavGraphBuilder.feature1Routes(navController: NavController) {
composable<Feature1> {
Feature1Screen(navController = navController)
}
composable<PrivateFeature1> {
PrivateFeature1Screen()
}
}
Here, Feature1Screen is publicly accessible through the Feature1 route, while PrivateFeature1Screen remains an internal screen only navigable within the module.
- feature2
Similarly, the f_eature2_ module follows the same pattern, defining its navigation structure within Feature2Routes:
<button data-code="fun NavGraphBuilder.feature2Routes(navController: NavController) {
composable {
Feature2Screen(navController = navController)
}
composable {
PrivateFeature2Screen()
}
}” data-toggle-ms=”1200″ aria-label=”Copy code” data-nosnippet=”” class=”code-copy-btn absolute right-3 top-3 size-8 p-1.5 flex items-center justify-center bg-black/50 text-white rounded-md transition-colors duration-200 cursor-pointer hover:bg-black/70″>
fun NavGraphBuilder.feature2Routes(navController: NavController) {
composable<Feature2> {
Feature2Screen(navController = navController)
}
composable<PrivateFeature2> {
PrivateFeature2Screen()
}
}
fun NavGraphBuilder.feature2Routes(navController: NavController) {
composable<Feature2> {
Feature2Screen(navController = navController)
}
composable<PrivateFeature2> {
PrivateFeature2Screen()
}
}
Feature2Screen is publicly accessible through the Feature2 route, while PrivateFeature2Screen remains encapsulated within the module.
Each module registers its routes within the main NavHost in the app module, ensuring that navigation remains modular while integrating all features into the global navigation system.
Benefits of This Architecture
- Modularity: Each feature is encapsulated in its own module, making maintenance and scalability easier.
- Encapsulation: Only necessary routes are exposed publicly, reducing unnecessary coupling.
- Ease of Testing: Since modules are decoupled, they can be tested independently.
- Scalability: New features can be added without modifying the existing structure.
Conclusion
This Jetpack Navigation-based modular navigation system offers a scalable and maintainable solution for large Android applications. Thanks to the separation of responsibilities and route encapsulation, each module is independent and can evolve without affecting the rest of the application. This approach is ideal for large teams and long-term projects where modularity is key.
You can see the complete project here 👀
What do you think of this architecture?🤔 Have you implemented something similar in your projects? Leave your thoughts in the comments!
