🧛 Fragula is a swipe-to-dismiss extension for navigation component library for Android

Last update: Jul 20, 2022

Fragula 2

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.

Android CI License Android Arsenal

Showcase


Table of Contents

  1. Gradle Dependency
  2. The Basics
  3. More Options
    1. Destination Arguments
    2. Multiple BackStacks
  4. Swipe Direction
  5. Swipe Transitions
  6. Theming

Gradle Dependency

MavenCentral

Add this to your module’s build.gradle file:

dependencies {
  ...
  implementation 'com.fragula2:fragula-core:2.2'
}

The fragula-core module contains everything you need to get started with the library. It contains all core and normal-use functionality.


The Basics

First, you need to replace NavHostFragment with FragulaNavHostFragment in your layout:

">

<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.fragula2.FragulaNavHostFragment" 
    android:id="@+id/nav_host"
    app:navGraph="@navigation/nav_graph"
    app:defaultNavHost="true" />

Second, you need to replace your destinations in graph with as shown below:

... ">

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/detailFragment">

    <swipeable
        android:id="@+id/detailFragment"
        android:name="com.example.fragula.DetailFragment"
        android:label="DetailFragment"
        tools:layout="@layout/fragment_detail" />

    ...
    
navigation>

Finally, you need to set opaque background and layout direction flag to your fragment’s root layout to avoid any issues with swipe animation.

... ">

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:colorBackground"
    android:layoutDirection="local">
    
    ...
    
androidx.constraintlayout.widget.ConstraintLayout>

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.

First, add an argument to the destination:

">
<swipeable 
    android:id="@+id/detailFragment"
    android:name="com.example.fragula.DetailFragment">
     <argument
         android:name="itemId"
         app:argType="string" />
 swipeable>

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.

">

<androidx.fragment.app.FragmentContainerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.fragula2.FragulaNavHostFragment" 
    android:id="@+id/nav_host" 
    app:swipeDirection="top_to_bottom"
    app:navGraph="@navigation/nav_graph"
    app:defaultNavHost="true" />

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.

class DetailFragment : Fragment(R.layout.fragment_detail) {
   
    private lateinit var swipeController: SwipeController
    private lateinit var swipeListener: OnSwipeListener
    
    override fun onViewCreated(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)
    }
   
    override fun onDestroyView() {
        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:

... ... ... #000000 0.10 1.3 300 ">
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">...item>
    <item name="colorPrimaryDark">...item>
    <item name="colorAccent">...item>

    
    <item name="fgl_dim_color">#000000item>

    
    <item name="fgl_dim_amount">0.10item>

    
    <item name="fgl_parallax_factor">1.3item>
   
    
    <item name="fgl_anim_duration">300item>
   
style>

GitHub

https://github.com/massivemadness/Fragula
You might also like...

Bottom-App-Bar-with-Bottom-Navigation-in-Jetpack-compose-Android - Bottom App Bar with Bottom Navigation in Jetpack compose

Bottom-App-Bar-with-Bottom-Navigation-in-Jetpack-compose-Android - Bottom App Bar with Bottom Navigation in Jetpack compose

Bottom-App-Bar-with-Bottom-Navigation-in-Jetpack-compose-Android This is simple

Jul 11, 2022

New style for app design simple bottom navigation with side navigation drawer UI made in Jetpack Compose.😉😎

New style for app design simple bottom navigation with side navigation drawer UI made in Jetpack Compose.😉😎

BottomNavWithSideDrawer New style for app design simple bottom navigtaion with side navigation drawer UI made in Jetpack Compose. 😉 😎 (Navigation Co

Jul 6, 2022

Navigation Drawer Bottom Navigation View

Navigation Drawer Bottom Navigation View

LIVE #019 - Toolbar, Navigation Drawer e BottomNavigationView com Navigation Com

Jun 15, 2022

🎉 [Android Library] A light-weight library to easily make beautiful Navigation Bar with ton of 🎨 customization option.

🎉 [Android Library] A light-weight library to easily make beautiful Navigation Bar with ton of 🎨 customization option.

Bubble Navigation 🎉 A light-weight library to easily make beautiful Navigation Bars with a ton of 🎨 customization options. Demos FloatingTopBarActiv

Aug 10, 2022

A small navigation library for Android to ease the use of fragment transactions & handling backstack (also available for Jetpack Compose).

A small navigation library for Android to ease the use of fragment transactions & handling backstack (also available for Jetpack Compose).

A small navigation library for Android to ease the use of fragment transactions & handling backstack (also available for Jetpack Compose).

Jul 23, 2022

A simple navigation library for Android 🗺️

Enro 🗺️ A simple navigation library for Android "The novices’ eyes followed the wriggling path up from the well as it swept a great meandering arc ar

Aug 4, 2022

AndroidBriefActions - Android library for sending and observing non persistent actions such as showing a message; nice readable way to call navigation actions from ViewModel or Activity/Fragment.

implementation "com.vladmarkovic.briefactions:briefactions:$briefActionsVersion" Benefits Why use brief-actions library pattern: Prevent short-term ac

Jul 25, 2021

🛸Voyager is a pragmatic navigation library built for, and seamlessly integrated with, Jetpack Compose.

🛸Voyager is a pragmatic navigation library built for, and seamlessly integrated with, Jetpack Compose.

Voyager is a pragmatic navigation library built for, and seamlessly integrated with, Jetpack Compose.

Aug 9, 2022

A library that you can use for bottom navigation bar. Written with Jetpack Compose

A library that you can use for bottom navigation bar. Written with Jetpack Compose

FancyBottomNavigationBar A library that you can use for bottom navigation bar. W

Jul 27, 2022
Comments
  • 1. Jetpack Compose improvements

    Missing features

    1. [ ] Slide transition using NavController.popBackStack() method
    2. [ ] Changing the swipe direction
    3. [ ] Swipe position listener

    Bugfixes

    1. [ ] Slide animation repeats on recomposition (when changing device orientation)
    2. [ ] All of the backstack entries (screens) are rendered at the same time
    Reviewed by massivemadness at 2022-06-19 09:00
  • 2. BackStack issue with `` destinations

    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:

    1. Make <swipeable> your start destination
    2. Navigate to a <fragment> destination
    3. 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.

    Снимок экрана 2022-03-19 в 13 39 43 Снимок экрана 2022-03-19 в 13 41 00
    Reviewed by massivemadness at 2022-03-19 10:43
Navigation Component: THE BEST WAY to create navigation flows for your app
Navigation Component: THE BEST WAY to create navigation flows for your app

LIVE #017 - Navigation Component: A MELHOR FORMA de criar fluxos de navegação para o seu app! Código fonte do projeto criado na live #017, ensinando c

Jun 15, 2022
Dismiss your keyboard by tapping anywhere outside it.
Dismiss your keyboard by tapping anywhere outside it.

Keyboard Dismisser What is this? Keyboard Dismisser is a simple library that allows you to dismiss keyboard by tapping anywhere outside it. Currently

Nov 28, 2021
NavigationComponent-SendingData - Android Navigation Component : Pass value (arguments) in Fragments
NavigationComponent-SendingData - Android Navigation Component : Pass value (arguments) in Fragments

NavigationComponent-SendingData Android Navigation Component : Pass value (argum

Dec 28, 2021
NavigationSafeArgs - Passing data with Navigation Component in Android
NavigationSafeArgs - Passing data with Navigation Component in Android

Navigation SafeArgs Sample Passing data with Navigation Component in Android. Pr

Feb 13, 2022
A Simple App to implement Navigation Architecture Component.

Android Navigation codelab Content: https://codelabs.developers.google.com/codelabs/android-navigation/ License Copyright 2018 The Android Open Source

Nov 5, 2021
Alligator is a modern Android navigation library that will help to organize your navigation code in clean and testable way.

Alligator Alligator is a modern Android navigation library that will help to organize your navigation code in clean and testable way. Features Any app

Jul 7, 2022
[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).

Simple Stack Why do I want this? To make navigation to another screen as simple as backstack.goTo(SomeScreen()), and going back as simple as backstack

Aug 12, 2022
A sleek, out of the box, easy to understand and use, swipe gesture based Navigational Library for android.
A sleek, out of the box, easy to understand and use, swipe gesture based Navigational Library for android.

Facilis Swipe gesture based navigational library for Android. Watch Demo Video: Getting Started To get this project into your build: Gradle Add it in

Feb 15, 2022
Android multi-module navigation built on top of Jetpack Navigation Compose
Android multi-module navigation built on top of Jetpack Navigation Compose

MultiNavCompose Android library for multi-module navigation built on top of Jetpack Navigation Compose. The goal of this library is to simplify the se

Apr 16, 2022
DSC Moi University session on using Navigation components to simplify creating navigation flow in our apps to use best practices recommended by the Google Android Team

Navigation Components Navigate between destination using safe args How to use the navigation graph and editor How send data between destinations Demo

Feb 3, 2022