Android BottomNavigation startDestination looping onCreate Function

48 Views Asked by At

I have a very simple App with a MainActivity that holds a FragmentContainerView and a BottomNavigationView.

In the FragmentContainerView i want to display one of three Fragments. The Fragments only hold a single Text with "Fragment 1" etc.

To Navigate thru the App vie the BottomNavitaionView i want to use a navGraph.

The Problem is, that when i define a startDestination in my navGraph it triggers the onCreate Function of that specific startDestination Fragment in a endless loop.

MainActivity.kt

class MainActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController

    }
}

FirstFragment.kt (Same goes for SecondFragment and ThirdFragment)

class FirstFragment: Fragment(R.layout.fragment_first) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i("FirstFragment", "onCreate called")
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/simple_nav_graph"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemTextColor="@color/cardview_light_background"
        app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment"
        app:menu="@menu/menu_bottom_nav" />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/details_page_fragment_one"
    android:name="com.****.****.ui.fragone.FirstFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Fragment One"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</fragment>

simple_nav_graph.xml

<?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/simple_nav_graph"
    app:startDestination="@id/details_page_fragment_one">

    <fragment
        android:id="@+id/details_page_fragment_one"
        android:name="com.*.*.ui.fragone.FirstFragment"
        android:label="Fragment 1"
        tools:layout="@layout/fragment_first">
        <action
            android:id="@+id/action_details_page_fragment_one_to_details_page_fragment_two"
            app:destination="@id/details_page_fragment_two" />
        <action
            android:id="@+id/action_details_page_fragment_one_to_details_page_fragment_three"
            app:destination="@id/details_page_fragment_three" />
    </fragment>


    <fragment
        android:id="@+id/details_page_fragment_two"
        android:name="com.*.*.ui.fragtwo.SecondFragment"
        android:label="Fragment 2"
        tools:layout="@layout/fragment_second">
        <action
            android:id="@+id/action_details_page_fragment_two_to_details_page_fragment_three"
            app:destination="@id/details_page_fragment_three" />
        <action
            android:id="@+id/action_details_page_fragment_two_to_details_page_fragment_one"
            app:destination="@id/details_page_fragment_one" />
    </fragment>
    <fragment
        android:id="@+id/details_page_fragment_three"
        android:name="com.*.*.ui.fragthree.ThirdFragment"
        android:label="Fragment 3"
        tools:layout="@layout/fragment_third">
        <action
            android:id="@+id/action_details_page_fragment_three_to_details_page_fragment_two"
            app:destination="@id/details_page_fragment_two" />
        <action
            android:id="@+id/action_details_page_fragment_three_to_details_page_fragment_one"
            app:destination="@id/details_page_fragment_one" />
    </fragment>

</navigation>

If i do not include app:navGraph inside my FragmentContainerView the App starts with the bottomNavigation but without any Function. What am i missing here?

Logcat looks like:

2023-06-07 16:40:33.457 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.468 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.474 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.480 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.486 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.492 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.498 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.503 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.508 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.514 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.519 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.525 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.531 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.536 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.541 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.552 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.558 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.563 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.568 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.573 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.578 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.582 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.587 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.592 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.597 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.602 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.607 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.612 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.621 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.627 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.633 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.646 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.651 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.658 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.665 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.670 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
2023-06-07 16:40:33.676 22883-22883 FirstFragment                       com...r.automotivecafeapp  I  onCreate called
1

There are 1 best solutions below

0
JRG On

You are using <fragment> inside your Fragment's layout. This is the cause of your infinite loop. Use ConstraintLayout or any other ViewGroup in your Fragments layout.