Skip to content

Comments

NavEventSnippets as per the Doc#800

Open
praneethatchana wants to merge 2 commits intomainfrom
NavEventSnippets
Open

NavEventSnippets as per the Doc#800
praneethatchana wants to merge 2 commits intomainfrom
NavEventSnippets

Conversation

@praneethatchana
Copy link

NavEventSnippets as per the Navigation Event Starting DAC Docs

@praneethatchana praneethatchana self-assigned this Feb 17, 2026
@praneethatchana praneethatchana requested a review from a team as a code owner February 17, 2026 17:23
@snippet-bot
Copy link

snippet-bot bot commented Feb 17, 2026

Here is the summary of changes.

You are about to add 7 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @praneethatchana, 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 introduces a new file containing various code snippets for integrating and handling predictive back gestures using the NavigationEvent library within Jetpack Compose applications. These examples cover defining event handlers, registering them with dispatchers, managing their lifecycle, and dispatching custom navigation events, aligning with the Navigation Event Starting DAC Docs.

Highlights

  • Navigation Event Handler Definition: Introduced a code snippet demonstrating how to define a custom NavigationEventHandler to manage predictive back events, including onBackStarted, onBackProgressed, onBackCompleted, and onBackCancelled callbacks.
  • Handler Registration and Lifecycle: Provided a Composable function RegisterHandler illustrating the proper way to register a NavigationEventHandler with a NavigationEventDispatcher using DisposableEffect for lifecycle management, ensuring the handler is removed on dispose.
  • NavigationBackHandler Composable: Included a placeholder NavigationBackHandler Composable function, outlining its parameters for handling navigation back events with a NavigationEventState.
  • Dispatcher Ownership and Input: Demonstrated the implementation of NavigationEventDispatcherOwner for custom components and the creation of a NavigationEventInput to manually dispatch back events (backStarted, backProgressed, backCancelled, backCompleted).
  • Dispatcher Setup and Cleanup: Showcased snippets for setting up a dispatcher by adding a custom input and cleaning up the dispatcher by explicitly calling dispose() when the component is destroyed.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/NavEventSnippets.kt
    • Added a new file containing code snippets for handling predictive back events using NavigationEvent in Jetpack Compose.
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

The pull request introduces a new set of snippets for the androidx.navigationevent library, specifically for handling predictive back gestures. The snippets cover handler definition, registration, and custom input dispatching. I have identified a few areas for improvement, primarily focusing on Compose best practices (using remember) and consistency in thread safety annotations. These changes will ensure the snippets are both functional and follow recommended patterns for Android development.

import androidx.navigationevent.NavigationEventHandler
import androidx.navigationevent.NavigationEventInfo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The remember function is required to ensure that the NavigationEventHandler instance persists across recompositions. Please add the corresponding import.

Suggested change
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember

Comment on lines +19 to +39
val myHandler = object: NavigationEventHandler<NavigationEventInfo>(
initialInfo = NavigationEventInfo.None,
isBackEnabled = true
) {
override fun onBackStarted(event: NavigationEvent) {
// Prepare for the back event
}

override fun onBackProgressed(event: NavigationEvent) {
// Use event.progress for predictive animations
}

// This is the required method for final event handling
override fun onBackCompleted() {
// Complete the back event
}

override fun onBackCancelled() {
// Cancel the back event
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

In Jetpack Compose, objects created directly within a @Composable function are recreated on every recomposition. Wrapping the NavigationEventHandler in remember ensures that the same instance is reused, which is critical for maintaining internal state and preventing redundant registration/unregistration cycles when passed to effects like DisposableEffect.

    val myHandler = remember {
        object : NavigationEventHandler<NavigationEventInfo>(
            initialInfo = NavigationEventInfo.None,
            isBackEnabled = true
        ) {
            override fun onBackStarted(event: NavigationEvent) {
                // Prepare for the back event
            }

            override fun onBackProgressed(event: NavigationEvent) {
                // Use event.progress for predictive animations
            }

            // This is the required method for final event handling
            override fun onBackCompleted() {
                // Complete the back event
            }

            override fun onBackCancelled() {
                // Cancel the back event
            }
        }
    }

Comment on lines +93 to +95
public fun backCancelled() {
dispatchOnBackCancelled()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For consistency with the other event dispatch methods in this class (backStarted, backProgressed, and backCompleted), the backCancelled method should also be annotated with @MainThread.

    @MainThread
    public fun backCancelled() {
        dispatchOnBackCancelled()
    }

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.

1 participant