Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add navigation listener #132

Open
BioRyajenka opened this issue Feb 21, 2025 · 6 comments
Open

Add navigation listener #132

BioRyajenka opened this issue Feb 21, 2025 · 6 comments

Comments

@BioRyajenka
Copy link

Hi,

This is a request to add navigation listener to the lib.

I want to spawn analytics events on navigation and currently I do smth like:

navController.navigateAndSpawnAnalyticsEvent(dest)

But custom code is not convenient and having an ability to add listener would be great.

@vkatz
Copy link
Contributor

vkatz commented Feb 22, 2025

You can use extensions in this case, see:

class LocalExtension(val logMessage: String) : ContentExtension<Any?> {

@vkatz
Copy link
Contributor

vkatz commented Feb 22, 2025

Another option is smth liks

val nc = rememberNavController(..)
LaunchEffect(nc.current){ // current is a state, so every time you navigate it will changed and call code below 
     trackScreen(nc.current)
}
...

i may add smth to see if it was back or frorward navigation

@BioRyajenka
Copy link
Author

Ideally, I need to know both "from" and "to" screen, and both your suggestions allow to know the target destination only. Or am I missing smth? 🤔

@vkatz
Copy link
Contributor

vkatz commented Feb 22, 2025

both "from" & "to"... hmmm it might be a case...

@vkatz
Copy link
Contributor

vkatz commented Feb 25, 2025

@BioRyajenka Some updates on fast solution, found in the notes "why dont we need nav listeners"

var lastScreen by remember { mutableStateOf<NavDestination<*>?>(null) }
LaunchedEffect(navController.current) {
    val currentScreen = navController.current
    if(lastScreen != currentScreen){
        // track from lastScreen to currentScreen
        lastScreen = currentScreen
    }
}

You can combine it with Extensions api and reuse

@BioRyajenka
Copy link
Author

That would work, thanks!
Still not as easy as listener, if you consider adding them

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

No branches or pull requests

2 participants