0

Currently I'm trying to implement app where on startup is SplashScreenFragment and after several seconds I switch to screen with BottomNavigationView

<?xml version="1.0" encoding="utf-8"?>
<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/app_navigation"
    app:startDestination="@id/navigation_splash_screen">

    <fragment
        android:id="@+id/navigation_splash_screen"
        android:name="com.example.test.SplashScreenFragment"
        tools:layout="@layout/fragment_splash_screen">

        <action
            android:id="@+id/action_navigation_splash_to_feature1"
            app:destination="@id/feature1_navigation"
            app:popUpTo="@+id/navigation_splash_screen"
            app:popUpToInclusive="true" />
    </fragment>

    <fragment
        android:id="@+id/navigation_test_1"
        android:name="com.example.test.Fragment1" />

    <fragment
        android:id="@+id/navigation_test_2"
        android:name="com.example.test.Fragment2" />

<fragment
        android:id="@+id/navigation_test_3"
        android:name="com.example.test.Fragment3" />
</navigation>

Then I define bottom_nav_menu.xml as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@id/navigation_test_1"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/bottom_nav_title_feature1" />

    <item
        android:id="@id/navigation_test_2"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/bottom_nav_title_feature2" />

<item
        android:id="@id/navigation_test_3"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/bottom_nav_title_feature3" />
</menu>

Version of navigation components:

androidx.navigation:navigation-fragment-ktx:2.2.0
androidx.navigation:navigation-ui-ktx:2.2.0

Now after calling bottomNavigationView.setupWithNavController(findNavController(R.id.nav_host_fragment)) inside Activity where nav_host_fragment is defined inside activity layout all seems to work.

Issue is, when user pressing back button or navigate back and forward between two items of menu do not clean backstack, that means that after clicking to feature1 -> feature2 -> feature1 -> feature2, when user pressing back button navigation is as follows: feature1 -> feature2 -> feature1 -> outside of the app (cause of popUpToInclusive on SplashScreenFragment) while desired back navigation is: feature1 -> outside of the app.

Anyone successfully tried to use BottomNavigationView where first item of bottom navigation is not startDestionation of navigation graph?

Edited as it seems that issue is startDestination not included graphs

Jarda Havelik
  • 502
  • 1
  • 7
  • 18
  • navigation id should be same as id's u give them in menu directory where u create navigation items then it is gonna work, and about back icon, what back icon u r talking about ur toolbar's one or phone's one? – USMAN osman Feb 11 '20 at 15:28
  • @USMANosman sorry I ment back button, edited in question. What you suggest is change menu ids to navigation_id? You mean to graph id or to fragments inside included graphs? Cause I'm sure that they are same as included graphs ids, and when I tried to change it to fragment navigation ids navigation did not worked at all (see end of my question) – Jarda Havelik Feb 11 '20 at 15:37
  • Yeah that's what is was saying graph ids should be same as menu directory ids. any ways what's ur intention to do when u do back button do u wanna navigate to specific fragment of completely different activity or what? – USMAN osman Feb 11 '20 at 15:46
  • @USMANosman Ids: that exactly as in example in question, or I'm missing something? Behaviour: I would like same behaviour as with not including graphs (first back navigates to first item of bottom navigation, second back outside of the app) – Jarda Havelik Feb 11 '20 at 15:53
  • no you are not missing anything as long as navigation is working fine, cause right now i am also doing fragment stuff mine is working fine (navigates to first fragment then out), post ur activity code and associated layout as well where navigation stuff is happening. – USMAN osman Feb 11 '20 at 16:01
  • @USMANosman Okay I did some research and found out that issue is with navigation_splash_screen that is set as startDestination, in that case BottomNavigationView back navigation will broke, cause navigation_splash_screen is not part of bottom_navigation_menu. I need some kind of splashscreen cause client wants to display downloaded banners etc (just switching theme is not sufficient), whats preffered approach here? Separate activity and that navigate to Different activity with bottom navigation? – Jarda Havelik Feb 11 '20 at 16:14

0 Answers0