我正在使用 BottomNavigationView 在 Home 和 Meetings 片段之間切換。每次我在它們之間切換時,都會重新創建每個 Fragment。我在它們之間使用 SharedViewModel 作為共享的“meetingID”變數,但它與 Fragments 一起被銷毀和重新創建(更嚴重的是)。我正在使用 Jetpack 導航組件;我幾乎完全復制了此處的 Google 示例。但是,它仍然無法正常作業。
看看我的 onCreate 和 onSupportNavigateUp:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment)
as NavHostFragment
navController = navHostFragment.navController
setSupportActionBar(binding.toolBar)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment, R.id.meetingFragment
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.apply {
navController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id == R.id.loginFragment
|| destination.id == R.id.registerFragment
) {
bottomNavigation.visibility = View.GONE
} else {
bottomNavigation.visibility = View.VISIBLE
}
}
bottomNavigation.setupWithNavController(navController)
}
}
override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration)
}
我的 nav_graph 相當丑陋:
<?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/nav_graph"
app:startDestination="@id/loginFragment">
<fragment
android:id="@ id/loginFragment"
android:name="edu.fsu.equidistant.fragments.LoginFragment"
android:label="fragment_login"
tools:layout="@layout/fragment_login">
<action
android:id="@ id/action_loginFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:launchSingleTop="true"
app:popUpTo="@ id/nav_graph"
app:popUpToInclusive="true" />
<action
android:id="@ id/action_loginFragment_to_registerFragment"
app:destination="@id/registerFragment" />
</fragment>
<fragment
android:id="@ id/registerFragment"
android:name="edu.fsu.equidistant.fragments.RegisterFragment"
android:label="fragment_register"
tools:layout="@layout/fragment_register">
<action
android:id="@ id/action_registerFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:launchSingleTop="true"
app:popUpTo="@ id/nav_graph"
app:popUpToInclusive="true" />
<action
android:id="@ id/action_registerFragment_to_loginFragment"
app:destination="@id/loginFragment" />
</fragment>
<fragment
android:id="@ id/homeFragment"
android:name="edu.fsu.equidistant.fragments.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" >
<argument
android:name="email"
app:argType="string" >
</argument>
<argument
android:name="userId" >
</argument>
<action
android:id="@ id/action_homeFragment_to_loginFragment"
app:destination="@id/loginFragment" />
<action
android:id="@ id/action_homeFragment_to_profileFragment"
app:destination="@id/profileFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_enter_anim" />
</fragment>
<fragment
android:id="@ id/profileFragment"
android:name="edu.fsu.equidistant.fragments.ProfileFragment"
android:label="fragment_profile"
tools:layout="@layout/fragment_profile" >
<argument
android:name="userId" >
</argument>
<action
android:id="@ id/action_profileFragment_to_loginFragment"
app:destination="@id/loginFragment" />
<action
android:id="@ id/action_profileFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="@ id/meetingFragment"
android:name="edu.fsu.equidistant.fragments.MeetingFragment"
android:label="Meeting"
tools:layout="@layout/fragment_meeting" />
</navigation>
我將每個 Fragment 設定為 appBarConfiguration 的頂級,但沒有。. . 有什么建議?
uj5u.com熱心網友回復:
@ianhanniballake 的想法是正確的!
我愚蠢地使用 LoginFragment 作為我的主目的地來笨拙地做一些條件邏輯。快速重構,并將 HomeFragment 設定為實際的主目的地,它現在可以作業了。謝謝,@ianhanniballake。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/364617.html
