Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomNavigationView with Splash Screen (Architecture Navigation Components)

I have implemented navigation with bottomNavigationView as shown in graph.
Navigation works fine if main_graph is selected as "start Destination". However, if I select fragment_splash to be "start Destination", from which I navigate to main_graph using popUpToInclusive, BottomNavigation does not work as intended. (it gets totally messed up, fragments do not get destroyed when navigating between bottom icons etc)

My nested main_graph has its own "start Destination" which should be start destination for BottomNavigationView.

I follow Single Activity approach.

How to solve this problem? Thank you.

like image 664
KarmaObserver Avatar asked Nov 26 '18 01:11

KarmaObserver


1 Answers

just implement onNavigationItemSelected for the BottomNavigationView like this

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        NavOptions navOptions = new NavOptions.Builder()
                .setPopUpTo(R.id.nav_home, false)
                .build();

        navController.navigate(id,null, navOptions);

    }

In my case I have defined destination names as same as menu-item ids in the BottomNavigation

here is my graph xml

<?xml version="1.0" encoding="utf-8"?>
<navigation
    android:id="@+id/main_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"
    app:startDestination="@id/splashFragment">
    <fragment
        android:id="@+id/splashFragment"
        android:name="com.businesslinktrading.makanilebanon.SplashFragment"
        android:label="fragment_splash"
        tools:layout="@layout/fragment_splash">
        <action
            android:id="@+id/action_splashFragment_to_nav_home"
            app:destination="@id/navigation4"
            app:launchSingleTop="true"
            app:popUpTo="@+id/main_navigation"
            app:popUpToInclusive="true"/>
    </fragment>
    <navigation android:id="@+id/navigation4"
                app:startDestination="@id/nav_home">
        <fragment
            android:id="@+id/nav_emergancy_phones"
            android:name="com.businesslinktrading.makanilebanon.EmergenciesFragment"
            android:label="fragment_emergencies"
            tools:layout="@layout/fragment_emergencies"/>
        <fragment
            android:id="@+id/nav_moods"
            android:name="com.businesslinktrading.makanilebanon.MoodsFragment"
            android:label="fragment_moods"
            tools:layout="@layout/fragment_moods"/>
        <fragment
            android:id="@+id/nav_home"
            android:name="com.businesslinktrading.makanilebanon.ForYouFragment"
            android:label="fragment_for_you"
            tools:layout="@layout/fragment_for_you"/>
        <fragment
            android:id="@+id/nav_groups"
            android:name="com.businesslinktrading.makanilebanon.GroupsFragment"
            android:label="fragment_groups"
            tools:layout="@layout/fragment_groups"/>
        <fragment
            android:id="@+id/nav_parking"
            android:name="com.businesslinktrading.makanilebanon.ParkingFragment"
            android:label="fragment_parking"
            tools:layout="@layout/fragment_parking"/>
    </navigation>

</navigation> 
like image 137
Raafat Alhmidi Avatar answered Oct 06 '22 03:10

Raafat Alhmidi