|
18 | 18 |
|
19 | 19 | package com.example.compose.snippets.animations.sharedelement |
20 | 20 |
|
| 21 | +import android.util.Log |
21 | 22 | import androidx.activity.compose.PredictiveBackHandler |
22 | 23 | import androidx.compose.animation.AnimatedContent |
23 | 24 | import androidx.compose.animation.AnimatedVisibility |
@@ -653,19 +654,24 @@ fun CustomPredictiveBackHandle() { |
653 | 654 |
|
654 | 655 | PredictiveBackHandler(seekableTransitionState.currentState is Screen.Details) { progress -> |
655 | 656 | try { |
| 657 | + // Whilst a back gesture is in progress, backEvents will be fired for each progress |
| 658 | + // update. |
656 | 659 | progress.collect { backEvent -> |
657 | | - // code for progress |
| 660 | + // For each backEvent that comes in, we manually seekTo the reported back progress |
658 | 661 | try { |
659 | 662 | seekableTransitionState.seekTo(backEvent.progress, targetState = Screen.Home) |
660 | 663 | } catch (e: CancellationException) { |
661 | | - // ignore the cancellation |
| 664 | + // seekTo may be cancelled as expected, if animateTo or subsequent seekTo calls |
| 665 | + // before the current seekTo finishes, in this case, we ignore the cancellation. |
662 | 666 | } |
663 | 667 | } |
664 | | - // code for completion |
| 668 | + // Once collection has completed, we are either fully in the target state, or need |
| 669 | + // to progress towards the end. |
665 | 670 | seekableTransitionState.animateTo(seekableTransitionState.targetState) |
666 | 671 | } catch (e: CancellationException) { |
667 | | - // code for cancellation |
668 | | - seekableTransitionState.animateTo(seekableTransitionState.currentState) |
| 672 | + // When the predictive back gesture is cancelled, we snap to the end state to ensure |
| 673 | + // it completes its seeking animation back to the currentState |
| 674 | + seekableTransitionState.snapTo(seekableTransitionState.currentState) |
669 | 675 | } |
670 | 676 | } |
671 | 677 | val coroutineScope = rememberCoroutineScope() |
|
0 commit comments