Android BottomNavigation startDestination 循环 onCreate 函数

问题描述 投票:0回答:1

我有一个非常简单的应用程序,其中包含一个包含 FragmentContainerView 和 BottomNavigationView 的 MainActivity。

在 FragmentContainerView 中,我想显示三个片段之一。片段仅包含带有“片段 1”等的单个文本。

要通过 BottomNavitaionView 浏览应用程序,我想使用 navGraph。

问题是,当我在 navGraph 中定义 startDestination 时,它会无限循环地触发该特定 startDestination 片段的 onCreate 函数。

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(SecondFragment 和 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>

如果我不将 app:navGraph 包含在我的 FragmentContainerView 中,则该应用程序从 BottomNavigation 开始,但没有任何功能。我在这里缺少什么?

Logcat 看起来像:

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
android kotlin androidx android-navigation android-navigationview
1个回答
0
投票

您正在片段的布局中使用

<fragment>
。这就是你无限循环的原因。在 Fragments 布局中使用 ConstraintLayout 或任何其他 ViewGroup。

© www.soinside.com 2019 - 2024. All rights reserved.