{"id":18822393,"url":"https://github.com/moallemi/multinavhost","last_synced_at":"2025-08-20T12:31:16.307Z","repository":{"id":40517230,"uuid":"149959005","full_name":"moallemi/MultiNavHost","owner":"moallemi","description":"Separate back stack history for each tab in Bottom Navigation View using Android Navigation Architecture Component ","archived":false,"fork":false,"pushed_at":"2020-07-22T05:56:51.000Z","size":3037,"stargazers_count":229,"open_issues_count":4,"forks_count":27,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-12-10T00:42:05.002Z","etag":null,"topics":["android","bottom-navigation-view","navigation-component"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moallemi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-23T07:52:46.000Z","updated_at":"2024-08-20T20:31:30.000Z","dependencies_parsed_at":"2022-06-29T15:03:25.384Z","dependency_job_id":null,"html_url":"https://github.com/moallemi/MultiNavHost","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moallemi%2FMultiNavHost","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moallemi%2FMultiNavHost/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moallemi%2FMultiNavHost/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moallemi%2FMultiNavHost/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moallemi","download_url":"https://codeload.github.com/moallemi/MultiNavHost/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230423559,"owners_count":18223435,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","bottom-navigation-view","navigation-component"],"created_at":"2024-11-08T00:49:14.812Z","updated_at":"2024-12-19T11:11:59.225Z","avatar_url":"https://github.com/moallemi.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MultiNavHost\nThis sample provides an approach to separate back stack history for each tab in Bottom Navigation View using Android Navigation Architecture Component\n\nBottom Navigation View gives the user quick access to 3-5 top-level destinations in an Android app. The common architectural approach for such a top level navigation which is provided by the Android navigation component is that activity only knows one backstack.\nBut in some cases you need to have different back stack history for each tab in bottom navigation view like Instagram app. \n\nThis sample app shows the usage of the new Navigation Architecture Component in collaboration with the Bottom Navigation view with separate back stack history for each tab.\n\nAs you know when you are using Android Navigation Component you have to use a NavHostFragment as a container for your fragments:\n\n```xml\n\u003cfragment\n    android:id=\"@+id/mainNavFragment\"\n    android:name=\"androidx.navigation.fragment.NavHostFragment\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    app:defaultNavHost=\"true\"\n    app:navGraph=\"@navigation/nav_graph_main\" /\u003e\n```\n\nBecause `Navigation` class in navigation components use just one back stack for each graph, you have to use multiple `NavHostFragment` with a **single** navigation graph. So your main xml layout should look like this:\n\n```xml\n\u003cfragment\n    android:id=\"@+id/homeTab\"\n    android:name=\"androidx.navigation.fragment.NavHostFragment\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    app:defaultNavHost=\"false\" /\u003e\n\u003cfragment\n    android:id=\"@+id/dashboardTab\"\n    android:name=\"androidx.navigation.fragment.NavHostFragment\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    app:defaultNavHost=\"false\" /\u003e\n\u003cfragment\n    android:id=\"@+id/notificationsTab\"\n    android:name=\"androidx.navigation.fragment.NavHostFragment\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    app:defaultNavHost=\"false\" /\u003e\n```\n\nTo avoid creating multiple navigation_graph.xml files, we use only `navigation_graph_main` file and every destination and action must be defined here.\n\n```xml\n\u003cnavigation xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n    android:id=\"@+id/navigation_graph_main\"\u003e\n\u003c/navigation\u003e\n```\nThere is no need to define the `app:startDestination` in the navigation graph. We need\ndifferent start destination for each tab. So we handle start destination in `TabManager` class.\n\n```kotlin\nprivate val startDestinations = mapOf(\n    R.id.navigation_home to R.id.homeFragment,\n    R.id.navigation_dashboard to R.id.dashboardFragment,\n    R.id.navigation_notifications to R.id.notificationsFragment\n)\n```\n\nFor further information please review the sample app code.\n\n\u003cimg src=\"https://github.com/moallemi/MultiNavHost/blob/master/.github/demo.gif?raw=true\" width=\"540\"\u003e\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoallemi%2Fmultinavhost","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoallemi%2Fmultinavhost","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoallemi%2Fmultinavhost/lists"}