You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
look-and-feel implementation have some limitations. For example swiping entry doesn't share saved states with actual entry if you don't use key in rememberSaveable. And swipe gesture doens't work on top of horizontally scrollable container.
P.S. Implementation from clip is located in this folder. One that located in com.github.alexzhirkevich.navigation is an experimental thing with native iOS UINavigationController
He used detectHorizontalDragGestures and when onDragEnd, use navigator.pop() to go previous screen. This does not have the peek in back gesture as IOS have but its a great alternative in the mean time as they hopefully develop this feature.
Hi folks, sharing here an example using the PreCompose swipe back implementation in Voyager. Hopefully soon I can refine this code and bring it here again, but, for anyone that is being blocked by this, here is the snippet from Voyager Multiplatform sample that I did the implementation.
Screen.Recording.2023-10-14.at.18.17.54.mov
importandroidx.compose.animation.AnimatedContentimportandroidx.compose.animation.EnterTransitionimportandroidx.compose.animation.ExitTransitionimportandroidx.compose.animation.core.AnimationSpecimportandroidx.compose.animation.core.Springimportandroidx.compose.animation.core.VisibilityThresholdimportandroidx.compose.animation.core.springimportandroidx.compose.animation.core.tweenimportandroidx.compose.animation.slideInHorizontallyimportandroidx.compose.animation.slideOutHorizontallyimportandroidx.compose.animation.togetherWithimportandroidx.compose.foundation.gestures.Orientationimportandroidx.compose.foundation.layout.Boximportandroidx.compose.foundation.layout.BoxWithConstraintsimportandroidx.compose.foundation.layout.offsetimportandroidx.compose.material.DismissDirectionimportandroidx.compose.material.DismissStateimportandroidx.compose.material.DismissValueimportandroidx.compose.material.ExperimentalMaterialApiimportandroidx.compose.material.FixedThresholdimportandroidx.compose.material.ResistanceConfigimportandroidx.compose.material.SwipeableDefaultsimportandroidx.compose.material.ThresholdConfigimportandroidx.compose.material.rememberDismissStateimportandroidx.compose.material.swipeableimportandroidx.compose.runtime.Composableimportandroidx.compose.runtime.LaunchedEffectimportandroidx.compose.runtime.derivedStateOfimportandroidx.compose.runtime.getValueimportandroidx.compose.runtime.keyimportandroidx.compose.runtime.mutableStateOfimportandroidx.compose.runtime.rememberimportandroidx.compose.runtime.setValueimportandroidx.compose.ui.Modifierimportandroidx.compose.ui.draw.drawWithContentimportandroidx.compose.ui.graphics.Colorimportandroidx.compose.ui.graphics.graphicsLayerimportandroidx.compose.ui.input.pointer.pointerInputimportandroidx.compose.ui.platform.LocalDensityimportandroidx.compose.ui.platform.LocalLayoutDirectionimportandroidx.compose.ui.unit.Dpimportandroidx.compose.ui.unit.IntOffsetimportandroidx.compose.ui.unit.LayoutDirectionimportandroidx.compose.ui.unit.dpimportandroidx.compose.ui.zIndeximportcafe.adriel.voyager.core.stack.StackEventimportcafe.adriel.voyager.navigator.Navigatorimportkotlin.math.absoluteValueimportkotlin.math.roundToIntexpectval shouldUseSwipeBack:Boolean// on iOS, this would be true and on Android/Desktop, false/** * @param slideInHorizontally a lambda that takes the full width of the * content in pixels and returns the initial offset for the slide-in prev entry, * by default it returns -fullWidth/4. * For the best impression, lambda should return the save value as [NavTransition.pauseTransition] * @param spaceToSwipe width of the swipe space from the left side of screen. * Can be set to [Int.MAX_VALUE].dp to enable full-scene swipe * @param swipeThreshold amount of offset to perform back navigation * @param shadowColor color of the shadow. Alpha channel is additionally multiplied * by swipe progress. Use [Color.Transparent] to disable shadow * */
@OptIn(ExperimentalMaterialApi::class)
classSwipeProperties(
valslideInHorizontally: (fullWidth: Int) ->Int = { -it / 4 },
valspaceToSwipe:Dp = 10.dp,
valswipeThreshold:ThresholdConfig = FixedThreshold(56.dp),
valshadowColor:Color = Color.Black.copy(alpha = .25f),
valswipeAnimSpec:AnimationSpec<Float> = tween(),
)
@Composable
publicfunSampleApplication() {
Navigator(
screen =BasicNavigationScreen(index =0),
onBackPressed = { currentScreen ->println("Navigator: Pop screen #${(currentScreen asBasicNavigationScreen).index}")
true
}
) { navigator ->val supportSwipeBack = remember { shouldUseSwipeBack }
if(supportSwipeBack) {
VoyagerSwipeBackContent(navigator)
} else {
SlideTransition(navigator)
}
}
}
@Composable
@OptIn(ExperimentalMaterialApi::class)
privatefunVoyagerSwipeBackContent(navigator:Navigator) {
val exampleSwipeProperties = remember { SwipeProperties() }
val currentSceneEntry = navigator.lastItem
val prevSceneEntry = remember(navigator.items) { navigator.items.getOrNull(navigator.size -2) }
var prevWasSwiped by remember {
mutableStateOf(false)
}
LaunchedEffect(currentSceneEntry) {
prevWasSwiped =false
}
val dismissState = key(currentSceneEntry) {
rememberDismissState()
}
LaunchedEffect(dismissState.isDismissed(DismissDirection.StartToEnd)) {
if (dismissState.isDismissed(DismissDirection.StartToEnd)) {
prevWasSwiped =true
navigator.pop()
}
}
val showPrev by remember(dismissState) {
derivedStateOf {
dismissState.offset.value >0f
}
}
val visibleItems = remember(currentSceneEntry, prevSceneEntry, showPrev) {
if (showPrev) {
listOfNotNull(currentSceneEntry, prevSceneEntry)
} else {
listOfNotNull(currentSceneEntry)
}
}
val canGoBack = remember(navigator.size) { navigator.size >1 }
val animationSpec = remember {
spring(
stiffness =Spring.StiffnessMediumLow,
visibilityThreshold =IntOffset.VisibilityThreshold
)
}
// display visible items using SwipeItem
visibleItems.forEachIndexed { index, backStackEntry ->val isPrev = remember(index, visibleItems.size) {
index ==1&& visibleItems.size >1
}
AnimatedContent(
backStackEntry,
transitionSpec = {
if (prevWasSwiped) {
EnterTransition.None togetherWith ExitTransition.None
} else {
val (initialOffset, targetOffset) =when (navigator.lastEvent) {
StackEvent.Pop-> ({ size:Int->-size }) to ({ size:Int-> size })
else-> ({ size:Int-> size }) to ({ size:Int->-size })
}
slideInHorizontally(animationSpec, initialOffset) togetherWith
slideOutHorizontally(animationSpec, targetOffset)
}
},
modifier =Modifier.zIndex(
if (isPrev) {
0f
} else {
1f
},
),
) { screen ->SwipeItem(
dismissState = dismissState,
swipeProperties = exampleSwipeProperties,//actualSwipeProperties,
isPrev = isPrev,
isLast =!canGoBack,
) {
navigator.saveableState("swipe", screen) {
screen.Content()
}
}
}
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
privatefunSwipeItem(
dismissState:DismissState,
swipeProperties:SwipeProperties,
isPrev:Boolean,
isLast:Boolean,
modifier:Modifier = Modifier,
content: @Composable () ->Unit,
) {
CustomSwipeToDismiss(
state =if (isPrev) rememberDismissState() else dismissState,
spaceToSwipe = swipeProperties.spaceToSwipe,
enabled =!isLast,
dismissThreshold = swipeProperties.swipeThreshold,
modifier = modifier,
) {
Box(
modifier =Modifier
.takeIf { isPrev }
?.graphicsLayer {
translationX =
swipeProperties.slideInHorizontally(size.width.toInt())
.toFloat() -
swipeProperties.slideInHorizontally(
dismissState.offset.value.absoluteValue.toInt(),
)
}?.drawWithContent {
drawContent()
drawRect(
swipeProperties.shadowColor,
alpha = (1f- dismissState.progress.fraction) *
swipeProperties.shadowColor.alpha,
)
}?.pointerInput(0) {
// prev entry should not be interactive until fully appeared
} ?:Modifier,
) {
content.invoke()
}
}
}
@Composable
@ExperimentalMaterialApi
privatefunCustomSwipeToDismiss(
state:DismissState,
enabled:Boolean = true,
spaceToSwipe:Dp = 10.dp,
modifier:Modifier = Modifier,
dismissThreshold:ThresholdConfig,
dismissContent: @Composable () ->Unit,
) =BoxWithConstraints(modifier) {
val width = constraints.maxWidth.toFloat()
val isRtl =LocalLayoutDirection.current ==LayoutDirection.Rtlval anchors =mutableMapOf(
0f to DismissValue.Default,
width to DismissValue.DismissedToEnd,
)
val shift =with(LocalDensity.current) {
remember(this, width, spaceToSwipe) {
(-width + spaceToSwipe.toPx().coerceIn(0f, width)).roundToInt()
}
}
Box(
modifier =Modifier
.offset { IntOffset(x = shift, 0) }
.swipeable(
state = state,
anchors = anchors,
thresholds = { _, _ -> dismissThreshold },
orientation =Orientation.Horizontal,
enabled = enabled && state.currentValue ==DismissValue.Default,
reverseDirection = isRtl,
resistance =ResistanceConfig(
basis = width,
factorAtMin =SwipeableDefaults.StiffResistanceFactor,
factorAtMax =SwipeableDefaults.StandardResistanceFactor,
),
)
.offset { IntOffset(x =-shift, 0) }
.graphicsLayer { translationX = state.offset.value },
) {
dismissContent()
}
}
Reacted by Reuben Mangubat, Maksat Inkar, Viktor Nyblom, Muhammad Utsman, topmotree, Chirag Redij, Wesley, Shibasis Patnaik and Anthony Cordani
I don't think being Material 3 matter for this component, actually, unless Material (not 3) will be discontinued in the future, but I don't think this is the case.
By looking into the swipeable modifier that is in use, it is the only API that are material that we most use in this could. The swipeable modifier is, currently, a pretty simple implementation on top of draggable modifier, so, we could just copy it directly into voyager if is the case to not use Material or Material3 because currently, there is no Voyager APIs that uses material directly.
The offset modifiers were causing some temporary white screen glitchiness when programmatically manipulating the backstack (ie not via gesture).
This code is still problematic if you have nested navigators since the second Box that captures the gestures will always be above nested navigator UX. For that, in my application I've just resorted to not restricting the eligible space for a swipe:
Nested navigation is convenient for how I have our code structured (model classes that are reused by 4-5 screens and I want to dispose of when I dispose the navigator), so long-term if I want to restrict swipe space I can just not do nested navigation. I also don't use voyager fwiw, but have adapted @DevSrSouza 's solution for my homespun navigation.
Hi folks, sharing here an example using the PreCompose swipe back implementation in Voyager. Hopefully soon I can refine this code and bring it here again, but, for anyone that is being blocked by this, here is the snippet from Voyager Multiplatform sample that I did the implementation.
Screen.Recording.2023-10-14.at.18.17.54.mov
importandroidx.compose.animation.AnimatedContentimportandroidx.compose.animation.EnterTransitionimportandroidx.compose.animation.ExitTransitionimportandroidx.compose.animation.core.AnimationSpecimportandroidx.compose.animation.core.Springimportandroidx.compose.animation.core.VisibilityThresholdimportandroidx.compose.animation.core.springimportandroidx.compose.animation.core.tweenimportandroidx.compose.animation.slideInHorizontallyimportandroidx.compose.animation.slideOutHorizontallyimportandroidx.compose.animation.togetherWithimportandroidx.compose.foundation.gestures.Orientationimportandroidx.compose.foundation.layout.Boximportandroidx.compose.foundation.layout.BoxWithConstraintsimportandroidx.compose.foundation.layout.offsetimportandroidx.compose.material.DismissDirectionimportandroidx.compose.material.DismissStateimportandroidx.compose.material.DismissValueimportandroidx.compose.material.ExperimentalMaterialApiimportandroidx.compose.material.FixedThresholdimportandroidx.compose.material.ResistanceConfigimportandroidx.compose.material.SwipeableDefaultsimportandroidx.compose.material.ThresholdConfigimportandroidx.compose.material.rememberDismissStateimportandroidx.compose.material.swipeableimportandroidx.compose.runtime.Composableimportandroidx.compose.runtime.LaunchedEffectimportandroidx.compose.runtime.derivedStateOfimportandroidx.compose.runtime.getValueimportandroidx.compose.runtime.keyimportandroidx.compose.runtime.mutableStateOfimportandroidx.compose.runtime.rememberimportandroidx.compose.runtime.setValueimportandroidx.compose.ui.Modifierimportandroidx.compose.ui.draw.drawWithContentimportandroidx.compose.ui.graphics.Colorimportandroidx.compose.ui.graphics.graphicsLayerimportandroidx.compose.ui.input.pointer.pointerInputimportandroidx.compose.ui.platform.LocalDensityimportandroidx.compose.ui.platform.LocalLayoutDirectionimportandroidx.compose.ui.unit.Dpimportandroidx.compose.ui.unit.IntOffsetimportandroidx.compose.ui.unit.LayoutDirectionimportandroidx.compose.ui.unit.dpimportandroidx.compose.ui.zIndeximportcafe.adriel.voyager.core.stack.StackEventimportcafe.adriel.voyager.navigator.Navigatorimportkotlin.math.absoluteValueimportkotlin.math.roundToIntexpectval shouldUseSwipeBack:Boolean// on iOS, this would be true and on Android/Desktop, false/** * @param slideInHorizontally a lambda that takes the full width of the * content in pixels and returns the initial offset for the slide-in prev entry, * by default it returns -fullWidth/4. * For the best impression, lambda should return the save value as [NavTransition.pauseTransition] * @param spaceToSwipe width of the swipe space from the left side of screen. * Can be set to [Int.MAX_VALUE].dp to enable full-scene swipe * @param swipeThreshold amount of offset to perform back navigation * @param shadowColor color of the shadow. Alpha channel is additionally multiplied * by swipe progress. Use [Color.Transparent] to disable shadow * */
@OptIn(ExperimentalMaterialApi::class)
classSwipeProperties(
valslideInHorizontally: (fullWidth: Int) ->Int = { -it / 4 },
valspaceToSwipe:Dp = 10.dp,
valswipeThreshold:ThresholdConfig = FixedThreshold(56.dp),
valshadowColor:Color = Color.Black.copy(alpha = .25f),
valswipeAnimSpec:AnimationSpec<Float> = tween(),
)
@Composable
publicfunSampleApplication() {
Navigator(
screen =BasicNavigationScreen(index =0),
onBackPressed = { currentScreen ->println("Navigator: Pop screen #${(currentScreen asBasicNavigationScreen).index}")
true
}
) { navigator ->val supportSwipeBack = remember { shouldUseSwipeBack }
if(supportSwipeBack) {
VoyagerSwipeBackContent(navigator)
} else {
SlideTransition(navigator)
}
}
}
@Composable
@OptIn(ExperimentalMaterialApi::class)
privatefunVoyagerSwipeBackContent(navigator:Navigator) {
val exampleSwipeProperties = remember { SwipeProperties() }
val currentSceneEntry = navigator.lastItem
val prevSceneEntry = remember(navigator.items) { navigator.items.getOrNull(navigator.size -2) }
var prevWasSwiped by remember {
mutableStateOf(false)
}
LaunchedEffect(currentSceneEntry) {
prevWasSwiped =false
}
val dismissState = key(currentSceneEntry) {
rememberDismissState()
}
LaunchedEffect(dismissState.isDismissed(DismissDirection.StartToEnd)) {
if (dismissState.isDismissed(DismissDirection.StartToEnd)) {
prevWasSwiped =true
navigator.pop()
}
}
val showPrev by remember(dismissState) {
derivedStateOf {
dismissState.offset.value >0f
}
}
val visibleItems = remember(currentSceneEntry, prevSceneEntry, showPrev) {
if (showPrev) {
listOfNotNull(currentSceneEntry, prevSceneEntry)
} else {
listOfNotNull(currentSceneEntry)
}
}
val canGoBack = remember(navigator.size) { navigator.size >1 }
val animationSpec = remember {
spring(
stiffness =Spring.StiffnessMediumLow,
visibilityThreshold =IntOffset.VisibilityThreshold
)
}
// display visible items using SwipeItem
visibleItems.forEachIndexed { index, backStackEntry ->val isPrev = remember(index, visibleItems.size) {
index ==1&& visibleItems.size >1
}
AnimatedContent(
backStackEntry,
transitionSpec = {
if (prevWasSwiped) {
EnterTransition.None togetherWith ExitTransition.None
} else {
val (initialOffset, targetOffset) =when (navigator.lastEvent) {
StackEvent.Pop-> ({ size:Int->-size }) to ({ size:Int-> size })
else-> ({ size:Int-> size }) to ({ size:Int->-size })
}
slideInHorizontally(animationSpec, initialOffset) togetherWith
slideOutHorizontally(animationSpec, targetOffset)
}
},
modifier =Modifier.zIndex(
if (isPrev) {
0f
} else {
1f
},
),
) { screen ->SwipeItem(
dismissState = dismissState,
swipeProperties = exampleSwipeProperties,//actualSwipeProperties,
isPrev = isPrev,
isLast =!canGoBack,
) {
navigator.saveableState("swipe", screen) {
screen.Content()
}
}
}
}
}
@OptIn(ExperimentalMaterialApi::class)
@Composable
privatefunSwipeItem(
dismissState:DismissState,
swipeProperties:SwipeProperties,
isPrev:Boolean,
isLast:Boolean,
modifier:Modifier = Modifier,
content: @Composable () ->Unit,
) {
CustomSwipeToDismiss(
state =if (isPrev) rememberDismissState() else dismissState,
spaceToSwipe = swipeProperties.spaceToSwipe,
enabled =!isLast,
dismissThreshold = swipeProperties.swipeThreshold,
modifier = modifier,
) {
Box(
modifier =Modifier
.takeIf { isPrev }
?.graphicsLayer {
translationX =
swipeProperties.slideInHorizontally(size.width.toInt())
.toFloat() -
swipeProperties.slideInHorizontally(
dismissState.offset.value.absoluteValue.toInt(),
)
}?.drawWithContent {
drawContent()
drawRect(
swipeProperties.shadowColor,
alpha = (1f- dismissState.progress.fraction) *
swipeProperties.shadowColor.alpha,
)
}?.pointerInput(0) {
// prev entry should not be interactive until fully appeared
} ?:Modifier,
) {
content.invoke()
}
}
}
@Composable
@ExperimentalMaterialApi
privatefunCustomSwipeToDismiss(
state:DismissState,
enabled:Boolean = true,
spaceToSwipe:Dp = 10.dp,
modifier:Modifier = Modifier,
dismissThreshold:ThresholdConfig,
dismissContent: @Composable () ->Unit,
) =BoxWithConstraints(modifier) {
val width = constraints.maxWidth.toFloat()
val isRtl =LocalLayoutDirection.current ==LayoutDirection.Rtlval anchors =mutableMapOf(
0f to DismissValue.Default,
width to DismissValue.DismissedToEnd,
)
val shift =with(LocalDensity.current) {
remember(this, width, spaceToSwipe) {
(-width + spaceToSwipe.toPx().coerceIn(0f, width)).roundToInt()
}
}
Box(
modifier =Modifier
.offset { IntOffset(x = shift, 0) }
.swipeable(
state = state,
anchors = anchors,
thresholds = { _, _ -> dismissThreshold },
orientation =Orientation.Horizontal,
enabled = enabled && state.currentValue ==DismissValue.Default,
reverseDirection = isRtl,
resistance =ResistanceConfig(
basis = width,
factorAtMin =SwipeableDefaults.StiffResistanceFactor,
factorAtMax =SwipeableDefaults.StandardResistanceFactor,
),
)
.offset { IntOffset(x =-shift, 0) }
.graphicsLayer { translationX = state.offset.value },
) {
dismissContent()
}
}
Thank you very much, I tried to use it and found that the gesture triggers the previous page's lifecycle as soon as it starts, I hope it can be triggered again at the end of the gesture, hopefully it will be available soon! Thank you. @DevSrSouza
Okay I've been working on this for longer than I care to admit, but I am using this right now in my app (still in testing phase, but almost ready for release). The problem is that the animations in Jetpack compose do not support setting the progress manually (I think it's internal or private). So you'll have to do that all by yourself unfortunately. Also the example code above tries to combine the AnimatedContent with a custom one if it is swiping. While this could work, when the animation is done it would clear the all the remembers (onDispose will be called). In my version I fixed that, but unfortunately like a said before you have to define the animation yourself. Inside the Transition files you can edit the transition to your liking.
Now when I start to swipe, the previous page starts to refresh immediately. This problem bothers me. How can I refresh the previous page after the current page is closed by swiping? Thanks @rewhex
@liufeng382641424 You can check if you are on top of the navigator and only start refreshing when that happens. The current screen will not be on top while you are still swiping.
@kevinvanmierlo When I call replace to navigate in Android, I got following error.
If I push(not replace) error wasn't occurred.
How can I fix this error?
java.lang.IllegalArgumentException: Key presentation.MainScreenContainer:transition was used multiple times
at androidx.compose.runtime.saveable.SaveableStateHolderImpl$SaveableStateProvider$1$1$1.invoke(SaveableStateHolder.kt:89)
at androidx.compose.runtime.saveable.SaveableStateHolderImpl$SaveableStateProvider$1$1$1.invoke(SaveableStateHolder.kt:88)
at androidx.compose.runtime.DisposableEffectImpl.onRemembered(Effects.kt:83)
@briandr97 What are you replacing? Usually this happens because the same screen is being used. So usually to fix this you need to create a new screen with a new screen key.
@rewhex / @kevinvanmierlo Unfortunately, the solution doesn't seem to work anymore with Compose Multiplatform 1.7.0-alpha01. The transition works, but dragging doesn't move the screens while the dragging continues. I have tried to fix the problem but have had no luck so far.
@rewhex / @kevinvanmierlo Unfortunately, the solution doesn't seem to work anymore with Compose Multiplatform 1.7.0-alpha01. The transition works, but dragging doesn't move the screens while the dragging continues. I have tried to fix the problem but have had no luck so far.
I removed derivedStateOf from anchoredDraggableState and it works for me. Additionally, you need to define the variables snapAnimationSpec and decayAnimationSpec in AnchoredDraggableState. Here is the code I am using:
I amended @kevinvanmierlo / @rewhex's solutions with two tweaks; dropping them here in case anyone finds them useful. Beware: they're pretty hacky.
Their implementation triggers the gesture anywhere on the screen, whereas most iOS apps I've tried trigger them only at the left edge of the screen (an exception to this is Twitter). I'm no iOS expert so I don't know which feels more natural to users, but in my opinion making the entire screen draggable feels a bit brittle so I reduced the handle to just the edge.
To do so, I moved the anchoredDraggable to a child invisible Box which acts as the handle:
The downside of this is that you can't interact with anything behind the handle. This is a fair compromise for me, as my app doesn't contain interactable elements that close to the edge of the screen. Also, I've added .padding(top = 80.dp), leaving a 80dp space which in my app is occupied by the back button.
There's probably a more idiomatic way of achieving this 😆
The default anchorDraggable implementation has a flaw that bothers me: if you drag left-to-right but end your gesture right-to-left, the gesture still completes and goes back to the parent screen. To reproduce, you can try the following:
Drag from 0% to 80% in 1 second. Hold. Drag from 80% to 60% in half a second. Release.
In iOS, the above would have aborted the back gesture, because the momentum of the screen was left, which feels natural. However, if we bypass 40%, positionalThreshold triggers which completes the gesture and feels unresponsive and unnatural. The default Voyager BottomSheet also suffers from this.
To fix it, I pass a confirmValueChange that aborts the gesture if the last movement of the screen was tending left with sufficient velocity:
Reacted by Artem Mishin, IENGROUND, Azad Mamiyev, Adrián, Amal Nuritdinkhodzhaev, Oleksii Hromovych, Naimul Kabir, Ebisuzawa Kurumi, Qiu, pepsi-wyl and 1 more
Activity
alexzhirkevich commentedon May 17, 2023
look-and-feel implementation have some limitations. For example swiping entry doesn't share saved states with actual entry if you don't use
keyinrememberSaveable. And swipe gesture doens't work on top of horizontally scrollable container.P.S. Implementation from clip is located in this folder. One that located in com.github.alexzhirkevich.navigation is an experimental thing with native iOS UINavigationController
codlab commentedon Sep 21, 2023
Bumping this one as having this behaviour would mean a lot. I could start maybe try porting this but it'll take some time due to availability :)
rjmangubat23 commentedon Oct 6, 2023
Would like to know if there is anyone who has a workaround for this in the mean time?
rjmangubat23 commentedon Oct 14, 2023
As a workaround, pointed out by Angga Ardinata in the Kotlin slack channels
He used
detectHorizontalDragGesturesand whenonDragEnd, usenavigator.pop()to go previous screen. This does not have the peek in back gesture as IOS have but its a great alternative in the mean time as they hopefully develop this feature.Reference: https://developer.android.com/jetpack/compose/touch-input/pointer-input/understand-gestures
DevSrSouza commentedon Oct 14, 2023
Hi folks, sharing here an example using the PreCompose swipe back implementation in Voyager. Hopefully soon I can refine this code and bring it here again, but, for anyone that is being blocked by this, here is the snippet from Voyager Multiplatform sample that I did the implementation.
Screen.Recording.2023-10-14.at.18.17.54.mov
DevSrSouza commentedon Oct 15, 2023
I don't think being Material 3 matter for this component, actually, unless Material (not 3) will be discontinued in the future, but I don't think this is the case.
By looking into the
swipeablemodifier that is in use, it is the only API that arematerialthat we most use in this could. The swipeable modifier is, currently, a pretty simple implementation on top of draggable modifier, so, we could just copy it directly into voyager if is the case to not use Material or Material3 because currently, there is no Voyager APIs that uses material directly.aicameron10 commentedon Nov 17, 2023
Hi , when there is a bottom bar it disappears and draws the screen over it , any fix i can try ?
A navigator nested in a tab navigator.
brendanw commentedon Dec 18, 2023
for those trying @DevSrSouza solution, I amended
CustomSwipeToDismissto the below:The
offsetmodifiers were causing some temporary white screen glitchiness when programmatically manipulating the backstack (ie not via gesture).This code is still problematic if you have nested navigators since the second Box that captures the gestures will always be above nested navigator UX. For that, in my application I've just resorted to not restricting the eligible space for a swipe:
Nested navigation is convenient for how I have our code structured (model classes that are reused by 4-5 screens and I want to dispose of when I dispose the navigator), so long-term if I want to restrict swipe space I can just not do nested navigation. I also don't use
voyagerfwiw, but have adapted @DevSrSouza 's solution for my homespun navigation.liufeng382641424 commentedon Feb 19, 2024
Thank you very much, I tried to use it and found that the gesture triggers the previous page's lifecycle as soon as it starts, I hope it can be triggered again at the end of the gesture, hopefully it will be available soon! Thank you. @DevSrSouza
planasmultimedia commentedon Feb 21, 2024
Hi folks! I was trying to implement it but i have some glitches when coming back to the previous screen, like fast white screen
kevinvanmierlo commentedon Feb 23, 2024
Okay I've been working on this for longer than I care to admit, but I am using this right now in my app (still in testing phase, but almost ready for release). The problem is that the animations in Jetpack compose do not support setting the progress manually (I think it's internal or private). So you'll have to do that all by yourself unfortunately. Also the example code above tries to combine the AnimatedContent with a custom one if it is swiping. While this could work, when the animation is done it would clear the all the remembers (onDispose will be called). In my version I fixed that, but unfortunately like a said before you have to define the animation yourself. Inside the Transition files you can edit the transition to your liking.
The gist is here: https://gist.github.com/kevinvanmierlo/8e051c96c84de9f5c921912d28414038. There a quite a few files since I also added predictive back gesture for Android. If iOS is all you care about you can skip the Android part and just use AnimatedContent there.
Let me know what you think! The code could probably be better at a few places, but it took a while to get this working solid. So feedback is welcome.
eduruesta commentedon Mar 6, 2024
Hey! Do we have a solution for this "Support iOS-like back gesture navigation"?
Syer10 commentedon Mar 6, 2024
Yes, the comment above yours has a implementation you can use.
eduruesta commentedon Mar 6, 2024
Thanks!! i just tested and worked!!
3 remaining items
harry248 commentedon May 24, 2024
Thanks for the solutions. Will there still be official support in the Voyager Library? And if so, is it foreseeable when this will happen?
liufeng382641424 commentedon Jun 5, 2024
Now when I start to swipe, the previous page starts to refresh immediately. This problem bothers me. How can I refresh the previous page after the current page is closed by swiping? Thanks @rewhex
kevinvanmierlo commentedon Jul 9, 2024
@liufeng382641424 You can check if you are on top of the navigator and only start refreshing when that happens. The current screen will not be on top while you are still swiping.
briandr97 commentedon Jul 16, 2024
@kevinvanmierlo When I call
replaceto navigate in Android, I got following error.If I
push(not replace) error wasn't occurred.How can I fix this error?
kevinvanmierlo commentedon Jul 16, 2024
@briandr97 What are you replacing? Usually this happens because the same screen is being used. So usually to fix this you need to create a new screen with a new screen key.
briandr97 commentedon Jul 17, 2024
@kevinvanmierlo Thank you, I find the reason. It was my fault. Thank you for your answer
harry248 commentedon Jul 19, 2024
@rewhex / @kevinvanmierlo Unfortunately, the solution doesn't seem to work anymore with Compose Multiplatform 1.7.0-alpha01. The transition works, but dragging doesn't move the screens while the dragging continues. I have tried to fix the problem but have had no luck so far.
omaeewa commentedon Jul 22, 2024
I removed
derivedStateOffromanchoredDraggableStateand it works for me. Additionally, you need to define the variablessnapAnimationSpecanddecayAnimationSpecinAnchoredDraggableState. Here is the code I am using:harry248 commentedon Jul 22, 2024
@omaeewa Thanks, removing the
derivedStateOfwas the missing part. Really hope there will be some official solution soon.MeLlamoPablo commentedon Jul 31, 2024
I amended @kevinvanmierlo / @rewhex's solutions with two tweaks; dropping them here in case anyone finds them useful. Beware: they're pretty hacky.
Their implementation triggers the gesture anywhere on the screen, whereas most iOS apps I've tried trigger them only at the left edge of the screen (an exception to this is Twitter). I'm no iOS expert so I don't know which feels more natural to users, but in my opinion making the entire screen draggable feels a bit brittle so I reduced the handle to just the edge.
To do so, I moved the
anchoredDraggableto a child invisibleBoxwhich acts as the handle:animatedScreens.fastForEach { screen -> key(screen.screen.key) { navigator.saveableState("transition", screen.screen) { Box( Modifier .fillMaxSize() .animatingModifier(screen), ) { screen.screen.Content() Box( Modifier .fillMaxHeight() .width(16.dp) .padding(top = 80.dp) .then(if (screen == currentScreen) currentScreenModifier else Modifier), ) } } } }The downside of this is that you can't interact with anything behind the handle. This is a fair compromise for me, as my app doesn't contain interactable elements that close to the edge of the screen. Also, I've added
.padding(top = 80.dp), leaving a 80dp space which in my app is occupied by the back button.There's probably a more idiomatic way of achieving this 😆
The default
anchorDraggableimplementation has a flaw that bothers me: if you drag left-to-right but end your gesture right-to-left, the gesture still completes and goes back to the parent screen. To reproduce, you can try the following:Drag from 0% to 80% in 1 second. Hold. Drag from 80% to 60% in half a second. Release.
In iOS, the above would have aborted the back gesture, because the momentum of the screen was left, which feels natural. However, if we bypass 40%,
positionalThresholdtriggers which completes the gesture and feels unresponsive and unnatural. The default Voyager BottomSheet also suffers from this.To fix it, I pass a
confirmValueChangethat aborts the gesture if the last movement of the screen was tending left with sufficient velocity:D10NGYANG commentedon Dec 7, 2024
When I use this, the iOS BackHandler intercept return action becomes invalid, how should I fix it?
kevinvanmierlo commentedon Dec 13, 2024
I added something along these lines. Could probably be done better, but this is what I have right now:
And then just don't add the draggable modifier or set enabled to false using this:
D10NGYANG commentedon Dec 25, 2024
I want some screens use FadeTransition, but not't success on iOS
terrakok commentedon Jan 24, 2025
I implemented BackHandler support in the multiplatform compose (not released yet). It can be used here as well
JetBrains/compose-multiplatform-core#1771
pepsi-wyl commentedon Nov 20, 2025
Have you repaired it?