Fragula is a swipe-to-dismiss extension for navigation component library for Android. It is an adaptation of an earlier version created by @shikleev and now maintained in this repository.
Now if you open the app you'll see that you can swipe fragments like in Telegram, Slack and many other messaging apps.
More Options
Destination Arguments
In general, you should work with Fragula as if you would work with normal fragments. You should strongly prefer passing only the minimal amount of data between destinations, as the total space for all saved states is limited on Android.
Second, create a Bundle object and pass it to the destination using navigate() as shown below:
val bundle = bundleOf("itemId" to "123")
findNavController().navigate(R.id.detailFragment, bundle)
Finally, in your receiving destination’s code, use the getArguments() method to retrieve the Bundle and use its contents:
val textView = view.findViewById<TextView>(R.id.textViewItemId)
textView.text = arguments?.getString("itemId")
It's strongly recommended to use Safe Args plugin for navigating and passing data, because it ensures type-safety.
Multiple BackStacks
Currently multiple backstacks is not supported, which means you can’t safely use extensions such as setupWithNavController(...) without losing your current backstack.
This issue affects both BottomNavigationView and NavigationView widgets.
Swipe Direction
If you want to change the direction of swipe gesture, you can do that by setting app:swipeDirection="..." manually in your navigation container. This example below sets up vertical swipe direction.
You can use either left_to_right (default) or right_to_left for horizontal direction. For vertical direction you can use only top_to_bottom, the bottom_to_top is not supported due to internal ViewPager2 restrictions.
Note:If you having an issues with nested scrollable views, this appears to be a scroll issue in ViewPager2. Please follow Google’s example to solve this.
Swipe Transitions
You may want to know when the scrolling offset changes to make smooth transitions inside your fragment view. To start listening scroll events you need to retrieve SwipeController and set OnSwipeListener as shown below:
Note:Currently shared element transitions between destinations are not supported in any form.
classDetailFragment : Fragment(R.layout.fragment_detail) {
privatelateinitvar swipeController:SwipeControllerprivatelateinitvar swipeListener:OnSwipeListeneroverridefunonViewCreated(view:View, savedInstanceState:Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
swipeController = findSwipeController()
swipeListener =OnSwipeListener { position, positionOffset, positionOffsetPixels ->// TODO animate views using `positionOffset` or `positionOffsetPixels`.// the `position` points to the position of the fragment in backstack
}
swipeController.addOnSwipeListener(swipeListener)
}
overridefunonDestroyView() {
super.onDestroyView()
swipeController.removeOnSwipeListener(swipeListener)
}
}
Remember: you must remove the listener when the fragment view is destroyed.
Theming
In most of the cases there is no need to change any values, but if you wish to override these, there are attributes provided:
Please consider making a Pull Request if you are capable of doing so.
Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Describe the solution you'd like
A clear and concise description of what you want to happen.
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
Add any other context or screenshots about the feature request here.
Creating a SwipeBackNavigator remember to act more like the bottom sheet from accompanist. This means it won't be added to the provider repeatedly.
I ran into an issue when changing the theme where my app crashed. I looked at logcat and saw:
java.lang.IllegalStateException: Navigator com.fragula2.compose.SwipeBackNavigator@1665b1f is replacing an already attached com.fragula2.compose.SwipeBackNavigator@b83b225
Describe the solution you'd like
I hope apply scroll animation when call navController().navigateUp()
Describe alternatives you've considered
I found accompanist-navigation-animation:0.16.1, which does not have gestures, but does this issue: https://medium.com/androiddevelopers/animations-in-navigation-compose-36d48870776b
Describe the bug
There's an issue when navigating from <swipeable> to <fragment> destination, the fragment will not be added to the backstack, which makes impossible to go back using popBackStack() or navigateUp() methods.
To Reproduce
Steps to reproduce the behavior:
Make <swipeable> your start destination
Navigate to a <fragment> destination
Call popBackStack() to return to the previous fragment
Any ideas of how to fix
The issue is not exactly in SwipeBackNavigator, It's the default behavior of androidx.navigation.fragment.FragmentNavigator, which depends on the backstack size to decide whether it should add a transaction to the backstack or not.
bug
opened by massivemadness 0
Releases(2.4.1)
2.4.1(Dec 17, 2022)
ChangeLog
Fixed: Crash on recomposition (thanks to @jakepurple13, 5d63bf864d01255d17bbd9a7a294fab0974d86f5)