我有一个非常简单的应用程序,它有一个MainActivity,包含一个FragmentContainerView和一个BottomNavigationView。
在FragmentContainerView中,我想显示三个Fragments中的一个。Fragments只包含一个带有“Fragment 1”等的文本。
要通过应用程序浏览底部NavitaionView,我想使用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>
型
如果我没有在我的FragmentContainerView中包含app:navGraph,应用程序将以bottomNavigation开始,但没有任何Function。我在这里缺少了什么?
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
型
1条答案
按热度按时间ymdaylpp1#
您在Fragment的布局中使用了
<fragment>
。这是导致无限循环的原因。请在Fragment布局中使用ConstraintLayout或任何其他ViewGroup。