<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bincy Baby on Medium]]></title>
        <description><![CDATA[Stories by Bincy Baby on Medium]]></description>
        <link>https://medium.com/@bbincybbaby?source=rss-8e0740a6194b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*JNTTQvaqXIylHNq-kQmfLw.jpeg</url>
            <title>Stories by Bincy Baby on Medium</title>
            <link>https://medium.com/@bbincybbaby?source=rss-8e0740a6194b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:06:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bbincybbaby/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Bottom Navigation Menu: Similar back stack and back arrow action like Youtube, Instagram, Amazon…]]></title>
            <link>https://medium.com/@bbincybbaby/bottom-navigation-menu-similar-back-stack-and-back-arrow-action-like-youtube-instagram-amazon-46a22f6d2fed?source=rss-8e0740a6194b------2</link>
            <guid isPermaLink="false">https://medium.com/p/46a22f6d2fed</guid>
            <category><![CDATA[android-backstack]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[bottomnavigationview]]></category>
            <category><![CDATA[backstack]]></category>
            <category><![CDATA[navigation]]></category>
            <dc:creator><![CDATA[Bincy Baby]]></dc:creator>
            <pubDate>Sat, 06 May 2023 07:02:43 GMT</pubDate>
            <atom:updated>2024-07-27T04:58:28.589Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eAOQs4Ai6jsMXCgfKK-uPA.jpeg" /></figure><h3>Bottom Navigation Menu: Similar back stack and back arrow action like Youtube, Instagram, Amazon and more</h3><p>I was trying to create a similar back stack and back action like Youtube, Instagram, Amazon and more. I tried using a view pager, custom back stack and many custom lines of code still I was not happy with the end results. Handling the back stack was the major pain area. Starting with the navigation component version <em>2.4.0-alpha01</em> NavigationUI support multiple back stacks for specific tab (child fragments inside selected tab) without any code change, but supporting back action to get back to the previous tab is still unavailable. Maybe in future, the official library may support that feature too.</p><p>For example, if we have <strong>4 main</strong> fragments (tabs) <strong>A, B, C, and D</strong>, the startDestination is <strong>A</strong>. <strong>D</strong> has <strong>child</strong> fragments <strong>D1, D2, and D3.</strong> If the user navigates like <strong>A -&gt; B -&gt; C -&gt;D -&gt; D1 -&gt; D2-&gt; D3</strong>, if the user clicks the back button with the official library the navigation will be <strong>D3 -&gt; D2-&gt; D1-&gt; D</strong> followed by <strong>A</strong>. That means primary tabs <strong>B and C</strong> will not be in the back stack.</p><p>I have created this <a href="https://github.com/bbincybbaby/MultiStackBottomNavigationView">Github repo</a> to show what I did. I reached this answer with the following medium articles.</p><ul><li><a href="https://medium.com/r?url=https%3A%2F%2Fvedraj360.medium.com%2Fyoutube-like-backstack-in-jetpack-navigation-component-android-2537b446668d">https://medium.com/r?url=https%3A%2F%2Fvedraj360.medium.com%2Fyoutube-like-backstack-in-jetpack-navigation-component-android-2537b446668d</a></li><li><a href="https://medium.com/androiddevelopers/navigation-multiple-back-stacks-6c67ba41952f">https://medium.com/androiddevelopers/navigation-multiple-back-stacks-6c67ba41952f</a></li></ul><ol><li>Add navigation dependencies to the Gradle file</li></ol><pre>implementation &#39;androidx.navigation:navigation-runtime-ktx:2.5.3&#39;<br>implementation &#39;androidx.navigation:navigation-fragment-ktx:2.5.3&#39;<br>implementation &#39;androidx.navigation:navigation-ui-ktx:2.5.3&#39;</pre><p>2. Add FragmentContainerView and BottomNavigationView to the main activity layout file</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    android:orientation=&quot;vertical&quot;<br>    tools:context=&quot;.MainActivity&quot;&gt;<br><br>    &lt;androidx.fragment.app.FragmentContainerView<br>        android:id=&quot;@+id/nav_host_container&quot;<br>        android:name=&quot;androidx.navigation.fragment.NavHostFragment&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;0dp&quot;<br>        android:layout_weight=&quot;1&quot;<br>        app:defaultNavHost=&quot;true&quot;<br>        app:navGraph=&quot;@navigation/nav_graph&quot;<br>        tools:layout=&quot;@layout/fragment_about&quot; /&gt;<br><br>    &lt;com.google.android.material.bottomnavigation.BottomNavigationView<br>        android:id=&quot;@+id/bottom_nav&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        app:menu=&quot;@menu/bottom_nav&quot;/&gt;<br>&lt;/LinearLayout&gt;</pre><p>3. Create menu.xml for BottomNavigationView and nav_graph for FragmentContainerView.</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;<br>    &lt;item<br>        android:id=&quot;@+id/home&quot;<br>        android:icon=&quot;@drawable/ic_home&quot;<br>        android:contentDescription=&quot;@string/cd_home&quot;<br>        android:title=&quot;@string/title_home&quot; /&gt;<br>    &lt;item<br>        android:id=&quot;@+id/list&quot;<br>        android:icon=&quot;@drawable/ic_list&quot;<br>        android:contentDescription=&quot;@string/cd_list&quot;<br>        android:title=&quot;@string/title_list&quot; /&gt;<br>    &lt;item<br>        android:id=&quot;@+id/form&quot;<br>        android:icon=&quot;@drawable/ic_feedback&quot;<br>        android:contentDescription=&quot;@string/cd_form&quot;<br>        android:title=&quot;@string/title_register&quot; /&gt;<br>&lt;/menu&gt;</pre><p>Instead of having a <strong>single navigation graph</strong> each item in BottomNavigationView will have a <strong>separate navigation graph </strong>and all three included in the <strong>main navigation graph</strong></p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;navigation<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    android:id=&quot;@+id/nav_graph&quot;<br>    app:startDestination=&quot;@+id/home&quot;&gt;<br><br>    &lt;include app:graph=&quot;@navigation/home&quot;/&gt;<br>    &lt;include app:graph=&quot;@navigation/list&quot;/&gt;<br>    &lt;include app:graph=&quot;@navigation/form&quot;/&gt;<br><br>&lt;/navigation&gt;</pre><p>4. Link BottomNavigationView and FragmentContainerView with setupWithNavController</p><pre>val navHostFragment = supportFragmentManager.findFragmentById(<br>    R.id.nav_host_container<br>) as NavHostFragment<br>navController = navHostFragment.navController<br>bottomNavigationView = findViewById(R.id.bottom_nav)<br>bottomNavigationView.setupWithNavController(navController)</pre><p>5. So far we have implemented whatever is supported by the official library, that is subfragments inside each tab will have back-stack support, switching the tabs will not recreate the fragment, instead it will stay where we last left. Now we have to customize it to support back functionality on main tabs</p><pre>private var addToBackStack: Boolean = true<br>private lateinit var fragmentBackStack: Stack&lt;Int&gt;</pre><p>The <strong>fragmentBackStack</strong> will help us to save all the visited destinations in the stack &amp; <strong>addToBackStack</strong> is a checker which will help to determine if we want to add the current destination into the stack or not.</p><p>6. When <strong>navHostFragment</strong> changes the fragment we get a callback to <strong>addOnDestinationChangedListener</strong> and we check whether the fragment is already existing in the Stack or not. If not we will add to the top of the Stack, if yes we will swap the position to the Stack&#39;s top. As we are now using separate graphs for each tab the id in the <strong>addOnDestinationChangedListener</strong> and BottomNavigationView will be different, so we use <strong>findBottomBarIdFromFragment</strong> to find the BottomNavigationView item id from the destination fragment.</p><pre>navHostFragment.findNavController().addOnDestinationChangedListener { _, destination, _ -&gt;<br>    val bottomBarId = findBottomBarIdFromFragment(destination.id)<br>    if (!::fragmentBackStack.isInitialized){<br>        fragmentBackStack = Stack()<br>    }<br>    if (needToAddToBackStack &amp;&amp; bottomBarId!=null) {<br>        if (!fragmentBackStack.contains(bottomBarId)) {<br>            fragmentBackStack.add(bottomBarId)<br>        } else if (fragmentBackStack.contains(bottomBarId)) {<br>            if (bottomBarId == R.id.home) {<br>                val homeCount =<br>                    Collections.frequency(fragmentBackStack, R.id.home)<br>                if (homeCount &lt; 2) {<br>                    fragmentBackStack.push(bottomBarId)<br>                } else {<br>                    fragmentBackStack.asReversed().remove(bottomBarId)<br>                    fragmentBackStack.push(bottomBarId)<br>                }<br>            } else {<br>                fragmentBackStack.remove(bottomBarId)<br>                fragmentBackStack.push(bottomBarId)<br>            }<br>        }<br><br>    }<br>    needToAddToBackStack = true<br>}</pre><pre>private fun findBottomBarIdFromFragment(fragmentId:Int?):Int?{<br>    if (fragmentId!=null){<br>        val bottomBarId = when(fragmentId){<br>            R.id.register -&gt;{<br>                R.id.form<br>            }<br>            R.id.leaderboard -&gt; {<br>                R.id.list<br>            }<br>            R.id.titleScreen -&gt;{<br>                R.id.home<br>            }<br>            else -&gt; {<br>                null<br>            }<br>        }<br>        return bottomBarId<br>    } else {<br>        return null<br>    }<br>}</pre><p>7. And when the user clicks back we override the activity’s <strong>onBackPressed</strong> method(<strong>NB</strong>:onBackPressed is deprecated I will update the answer once I find a replacement for super.onBackPressed() inside override fun onBackPressed())</p><pre>override fun onBackPressed() {<br>    val bottomBarId = if (::navController.isInitialized){<br>        findBottomBarIdFromFragment(navController.currentDestination?.id)<br>    } else {<br>        null<br>    }<br>    if (bottomBarId!=null) {<br>        if (::fragmentBackStack.isInitialized &amp;&amp; fragmentBackStack.size &gt; 1) {<br>            if (fragmentBackStack.size == 2 &amp;&amp; fragmentBackStack.lastElement() == fragmentBackStack.firstElement()){<br>                finish()<br>            } else {<br>                fragmentBackStack.pop()<br>                val fragmentId = fragmentBackStack.lastElement()<br>                needToAddToBackStack = false<br>                bottomNavigationView.selectedItemId = fragmentId<br>            }<br>        } else {<br>            if (::fragmentBackStack.isInitialized &amp;&amp; fragmentBackStack.size == 1) {<br>                finish()<br>            } else {<br>                super.onBackPressed()<br>            }<br>        }<br>    } else super.onBackPressed()<br>}</pre><p>When the user clicks back we will pop the last fragment from Stack and set the selected item id in the bottom navigation view.</p><p>Full Github Repo -&gt; <a href="https://github.com/bbincybbaby/MultiStackBottomNavigationView">https://github.com/bbincybbaby/MultiStackBottomNavigationView</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=46a22f6d2fed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>