Skip to content

Conversation

@arnaudgiuliani
Copy link

This PR proposes Koin Navigation 3 integration usages:

Module navigation entry declaration (within Activity scope) with navigation function:

val profileModule = module {
    activityRetainedScope {
        navigation<Profile> { ProfileScreen() }
    }
}

val conversationModule = module {
    activityRetainedScope {
        navigation<ConversationList> {
            ConversationListScreen(
                onConversationClicked = { conversationDetail ->
                    get<Navigator>().goTo(conversationDetail)
                }
            )
        }

        navigation<ConversationDetail> { key ->
            ConversationDetailScreen(key) {
                get<Navigator>().goTo(Profile)
            }
        }
    }
}

Allow to inject directly the EntryProvider from Activity:

class KoinModularActivity : ComponentActivity(), AndroidScopeComponent {

    override val scope : Scope by activityRetainedScope()
    val navigator: Navigator by inject()

    override fun onCreate(savedInstanceState: Bundle?) {
        // ...

        setContent {
            Scaffold { paddingValues ->
                NavDisplay(
                    backStack = navigator.backStack,
                    modifier = Modifier.padding(paddingValues),
                    onBack = { navigator.goBack() },
                    // inject here
                    entryProvider = getEntryProvider()
                )
            }
        }
    }
}

Key Changes

New Files

  • KoinModularActivity.kt: Main activity demonstrating Koin-based modular navigation with koin-compose-navigation3 integration
  • AppModule.kt: Root Koin module that includes feature modules and provides the Navigator
  • ConversationModule.kt: Feature module with ConversationList and ConversationDetail screens (108 lines)
  • ProfileModule.kt: Feature module with Profile screen
  • CommonModule.kt: Shared Navigator class managing navigation backstack

Refactoring

  • Renamed ModularActivity → HiltModularActivity to distinguish between DI approaches
  • Updated KoinViewModelsActivity to properly handle Koin lifecycle (prevent multiple instances)

Configuration Updates

  • Added koin-compose-navigation3 dependency (v4.2.0-alpha1)
  • Updated Navigation3 to v1.0.0-beta01
  • Updated Koin to v4.2.0-alpha1
  • Updated README and RecipePickerActivity to document both DI approaches

Architecture

The implementation follows a modular pattern where:

  • Features (Conversation, Profile) are defined in separate modules
  • Each module registers navigation composables using Koin's navigation DSL
  • A shared Navigator class manages the backstack across features
  • The main activity uses getEntryProvider() to automatically discover and render navigation entries

@google-cla
Copy link

google-cla bot commented Nov 4, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @arnaudgiuliani, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request integrates Koin as a dependency injection framework for modular navigation within the Navigation3 library. It provides a complete example of how to structure an Android application with Koin-driven feature modules, each defining its own navigation entries. This offers developers a clear alternative to the existing Hilt-based modular navigation example, promoting flexibility in DI choices while maintaining a clean, modular architecture.

Highlights

  • Koin Navigation 3 Integration: Introduced Koin Navigation 3 integration, offering an alternative to Hilt for modular navigation within the Navigation3 library.
  • Modular Navigation Declaration: Implemented module-based navigation entry declarations using Koin's navigation function within activityRetainedScope for defining screens like Profile and ConversationList.
  • EntryProvider Injection: Enabled direct injection of EntryProvider from the ComponentActivity (e.g., KoinModularActivity) for seamless navigation setup.
  • New Koin Modular Architecture Files: Added several new Koin-specific files to demonstrate a modular architecture, including KoinModularActivity.kt, AppModule.kt, ConversationModule.kt, ProfileModule.kt, and CommonModule.kt for a shared Navigator.
  • Refactoring and Clarity: Refactored the existing Hilt-based modular activity by renaming ModularActivity to HiltModularActivity for better distinction between DI approaches.
  • Koin Lifecycle Management: Updated Koin initialization logic in KoinViewModelsActivity to explicitly stop any existing Koin instances before starting a new one, preventing multiple Koin contexts.
  • Dependency Updates: Upgraded koin-compose-navigation3 dependency, Navigation3 to 1.0.0-beta01, and Koin to 4.2.0-alpha1.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully integrates Koin for modular navigation, providing a valuable example alongside the existing Hilt implementation. The new modules and activity are well-structured. My review includes a few suggestions to improve the code. The most important feedback relates to the Koin initialization strategy, which, while functional for this sample app, follows a pattern that is not recommended for production applications. I've also noted a documentation comment that appears to be a copy-paste from the Hilt example and should be corrected for clarity.

Comment on lines +46 to +53
if (KoinPlatform.getKoinOrNull() != null) {
stopKoin()
}
// The startKoin block should be placed in Application.onCreate.
startKoin {
androidContext(this@KoinModularActivity)
modules(appModule)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Initializing Koin within an Activity's onCreate method, especially with stopKoin(), is not a recommended practice for production applications. It can lead to performance issues and unpredictable behavior if multiple components rely on a stable Koin container. While this approach may be necessary for the isolated nature of these recipe examples, it's crucial to highlight that in a real-world app, startKoin should be called only once, typically in your Application class's onCreate method. Please consider adding a more prominent warning in the KDoc or a code comment to prevent developers from copying this pattern into production code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part is to help local sample, of course not advised to to so in normal app setup

Comment on lines +43 to 55
//prevent any already launched Koin instance with other config
if (KoinPlatform.getKoinOrNull() != null) {
stopKoin()
}
// The startKoin block should be placed in Application.onCreate.
if (GlobalContext.getOrNull() == null) {
GlobalContext.startKoin {
androidContext(this@KoinViewModelsActivity)
modules(
module {
viewModelOf(::RouteBViewModel)
}
)
}
startKoin {
androidContext(this@KoinViewModelsActivity)
modules(
module {
viewModelOf(::RouteBViewModel)
}
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the other Koin activity, re-initializing Koin in onCreate by calling stopKoin() and startKoin() is an anti-pattern. This can cause performance degradation and unexpected behavior in a real application where a consistent dependency graph is expected. It's a best practice to initialize Koin once in the Application class. Please add a comment to warn developers against using this pattern in production code.

…ularActivity.kt

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@arnaudgiuliani
Copy link
Author

done CLA update for my profile

Copy link

@pedrofsn pedrofsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@xephosbot
Copy link

Is it possible to pass metadata to the entry using the new navigation dsl?

Copy link

@danysantiago danysantiago left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Thanks!

* A shared `Navigator` class manages the backstack.
*
* The `appModule` includes the feature modules, creates the `Navigator` with a start destination,
* and makes it available for injection into the `KoinModularActivity` and feature modules.
Copy link

@danysantiago danysantiago Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add one more sentence about how the NavDisplay's is then setup with an EntryProvider from's Koin feature extension koin-compose-navigation3 (i.e. the getEntryProvider Koin function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants