【发布时间】:2020-01-02 09:30:57
【问题描述】:
我已经用nav graph 设置了底部导航,以最基本的方式 -
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>
【问题讨论】:
-
如果可能的话,你能分享给我你的代码吗?仅查看您提供的单行代码,我无法弄清楚问题所在。
标签: android android-architecture-navigation