-
Notifications
You must be signed in to change notification settings - Fork 339
NavEventSnippets as per the Doc #800
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| @file:Suppress("unused", "UNUSED_VARIABLE") | ||
|
|
||
| package com.example.compose.snippets.predictiveback | ||
|
|
||
| import androidx.annotation.MainThread | ||
| import androidx.navigationevent.NavigationEvent | ||
| import androidx.navigationevent.NavigationEventHandler | ||
| import androidx.navigationevent.NavigationEventInfo | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.DisposableEffect | ||
| import androidx.navigationevent.NavigationEventDispatcher | ||
| import androidx.navigationevent.NavigationEventDispatcherOwner | ||
| import androidx.navigationevent.NavigationEventInput | ||
| import androidx.navigationevent.compose.NavigationEventState | ||
|
|
||
| @Composable | ||
praneethatchana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private fun HandlingBackEvents() { | ||
praneethatchana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // [START android_compose_predictiveback_navevent_handler] | ||
| 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 | ||
| } | ||
| } | ||
|
Comment on lines
+35
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Jetpack Compose, objects created directly within a 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
}
}
}
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @praneethatchana since this portion of the code is multiplatform, I would ignore this suggestion and instead remove the "@ Composable" on line 32. |
||
| // [END android_compose_predictiveback_navevent_handler] | ||
| } | ||
|
|
||
| // [START android_compose_predictiveback_navevent_register_handler] | ||
| @Composable | ||
| fun RegisterHandler( | ||
| navigationEventDispatcher: NavigationEventDispatcher, | ||
| myHandler: NavigationEventHandler<*> | ||
| ) { | ||
| DisposableEffect(navigationEventDispatcher, myHandler) { | ||
| navigationEventDispatcher.addHandler(myHandler) | ||
| onDispose { | ||
| myHandler.remove() | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_predictiveback_navevent_register_handler] | ||
|
|
||
|
|
||
|
|
||
| // [START android_compose_predictiveback_navevent_NavigationEventHandler] | ||
| @Composable | ||
praneethatchana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public fun NavigationBackHandler( | ||
| state: NavigationEventState<out NavigationEventInfo>, | ||
| isBackEnabled: Boolean = true, | ||
| onBackCancelled: () -> Unit = {}, | ||
| onBackCompleted: () -> Unit, | ||
| ){ | ||
|
|
||
| } | ||
| // [END android_compose_predictiveback_navevent_NavigationEventHandler] | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @praneethatchana add a new code sample here on how to handle back using NavigationEventTransitionState with NavigationBackHandler. |
||
|
|
||
| // [START android_compose_predictiveback_navevent_NavigationEvent_dispatcher_owner] | ||
| class MyComponent: NavigationEventDispatcherOwner { | ||
| override val navigationEventDispatcher: NavigationEventDispatcher = | ||
| NavigationEventDispatcher() | ||
| } | ||
| // [END android_compose_predictiveback_navevent_NavigationEvent_dispatcher_owner] | ||
|
|
||
|
|
||
| // [START android_compose_predictiveback_navevent_navigation_event_input] | ||
| public class MyInput : NavigationEventInput() { | ||
| @MainThread | ||
| public fun backStarted(event: NavigationEvent) { | ||
| dispatchOnBackStarted(event) | ||
| } | ||
|
|
||
| @MainThread | ||
| public fun backProgressed(event: NavigationEvent) { | ||
| dispatchOnBackProgressed(event) | ||
| } | ||
|
|
||
| public fun backCancelled() { | ||
| dispatchOnBackCancelled() | ||
| } | ||
|
Comment on lines
+109
to
+111
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| @MainThread | ||
| public fun backCompleted() { | ||
| dispatchOnBackCompleted() | ||
| } | ||
| } | ||
| // [END android_compose_predictiveback_navevent_navigation_event_input] | ||
|
|
||
| // TODO: We then need to provide that input to our dispatcher: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @praneethatchana these are so short what do you think about removing them entirely and just mentioning them in-line in the documentation? I'm also ok if you would rather keep them, but perhaps first remove the TODO. |
||
| // [START android_compose_predictiveback_navevent_register_input] | ||
| fun setupDispatcher() { | ||
| val myComponent = MyComponent() | ||
| val myInput = MyInput() | ||
|
|
||
| // Register the custom input with the dispatcher | ||
| myComponent.navigationEventDispatcher.addInput(myInput) | ||
| } | ||
| // [END android_compose_predictiveback_navevent_register_input] | ||
|
|
||
| // [START android_compose_predictiveback_navevent_dispose] | ||
| fun cleanupDispatcher(myComponent: MyComponent) { | ||
| // Explicitly remove the dispatcher from the hierarchy when the component is destroyed | ||
| myComponent.navigationEventDispatcher.dispose() | ||
| } | ||
| // [END android_compose_predictiveback_navevent_dispose] | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
rememberfunction is required to ensure that theNavigationEventHandlerinstance persists across recompositions. Please add the corresponding import.