导航图(nav graph)
以最基本的方式设置了底部导航(bottom navigation)。NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)
当从被声明为startDestination
的片段导航时,该片段永远不会被销毁(仅暂停),而所有其他片段在导航离开时都被销毁。
(我需要它被销毁,以便与之关联的viewModel中可以调用onCleared()
回调函数)。
有任何想法吗?或如何更改此行为?
导航:
<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/navigation"
app:startDestination="@id/drawingFragment">
<fragment
android:id="@+id/controllerFragment"
android:name="com.example.android.myApp.ControllerFragment"
android:label="fragment_controller"
tools:layout="@layout/fragment_controller" >
<action
android:id="@+id/action_controllerFragment_to_drawingFragment"
app:destination="@id/drawingFragment" />
</fragment>
<fragment
android:id="@+id/drawingFragment"
android:name="com.example.android.myApp.DrawingFragment"
android:label="fragment_drawing"
tools:layout="@layout/fragment_drawing" >
<action
android:id="@+id/action_drawingFragment_to_clippingFragment"
app:destination="@id/clippingFragment"
app:launchSingleTop="true"
app:popUpTo="@+id/drawingFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/clippingFragment"
android:name="com.example.android.myApp.ClippingFragment"
android:label="fragment_clipping"
tools:layout="@layout/fragment_clipping" />
主活动:
class MainActivity : AppCompatActivity() {
private lateinit var navHostFragment: NavHostFragment
private lateinit var bottomNavigationView: BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpNavigation()
}
fun setUpNavigation(){
bottomNavigationView = findViewById(R.id.bttm_nav)
navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)}
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">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bttm_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemTextAppearanceActive="@style/bottomNaActive"
app:itemTextAppearanceInactive="@style/bottomNavInactive"
app:layout_constraintBottom_toBottomOf="@+id/nav_host_fragment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>