Android Fragments 如何使用导航组件保留片段?

8hhllhi2  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(150)

我试图创建一个侧导航抽屉式的应用程序使用最新的导航组件库('androidx.navigation:navigation-fragment-ktx:2.5.3''androidx.navigation:navigation-ui-ktx:2.5.3')我试图遵循创建导航抽屉的基本模板,但每次单击菜单项,它都会破坏当前片段并创建一个新片段。此外,当按下back时,我们会返回到start fragment,但start fragment也会重新创建,所以我的问题是是否有可能保留一个片段而不引起重复的销毁/创建循环?
我知道片段的“状态”可以使用activity viewmodel或者保存示例状态来保留,但是在某些时候,使用一个单独的片段而不是重新创建它会更有好处。google的following advance navigation sample确实保留了导航状态,但是我的当前版本似乎没有这样做,为什么?
代码:

//navgraph sidenav advance.xml

<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"
    app:startDestination="@+id/nav_home">

    <navigation android:id="@+id/nav_home" app:startDestination="@+id/frag_home">
        <fragment
            android:id="@+id/frag_home"
            android:name="work.curioustools.fragmentpracticalsamples.side_navigation.HomeFragment"
            android:label="@string/menu_home"
            tools:layout="@layout/fragment_side_navigation" />
    </navigation>

    <navigation android:id="@+id/nav_gallery" app:startDestination="@+id/frag_gallary">
        <fragment
            android:id="@+id/frag_gallary"
            android:name="work.curioustools.fragmentpracticalsamples.side_navigation.GalleryFragment"
            android:label="@string/menu_gallery"
            tools:layout="@layout/fragment_side_navigation" />

    </navigation>

    <navigation android:id="@+id/nav_slideshow" app:startDestination="@+id/frag_slideshow">
        <fragment
            android:id="@+id/frag_slideshow"
            android:name="work.curioustools.fragmentpracticalsamples.side_navigation.SlideshowFragment"
            android:label="@string/menu_slideshow"
            tools:layout="@layout/fragment_side_navigation" />
    </navigation>
    
</navigation>
activity sidenav default.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"

    tools:ignore="ContentDescription"
    tools:openDrawer="start">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar">
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                tools:navigationIcon="@drawable/ic_sidenav"
                app:title="@string/app_name"
                app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light" />

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.fragment.app.FragmentContainerView
                android:id="@+id/fragment_navhost_default"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/navgraph_sidenav_advance" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            app:srcCompat="@android:drawable/ic_dialog_email"
             />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        tools:layout_width="100dp"
        app:menu="@menu/menu_side_navigation" />
</androidx.drawerlayout.widget.DrawerLayout>
// activity : SideNavigationDrawerDefaultActivity

class SideNavigationDrawerDefaultActivity : AppCompatActivity() {
    private  val binding by lazy {  ActivitySideNavigationDefaultBinding.inflate(layoutInflater) }
    private  val appBarConfiguration by lazy { AppBarConfiguration(setOf(R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), binding.drawerLayout) }
    private  val navController by lazy {
        val navHost = supportFragmentManager.findFragmentById(R.id.fragment_navhost_default) as NavHostFragment
        navHost.navController
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        //setupActionBar
        setSupportActionBar(binding.toolbar)
        setupActionBarWithNavController(navController, appBarConfiguration)

        //setup side drawer
        binding.navView.setupWithNavController(navController)
        binding.fab.setOnClickListener { snack("hello",it) }
    }

    override fun onSupportNavigateUp() = navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()

}
kr98yfug

kr98yfug1#

您必须为每个导航抽屉项目创建单独的导航图文件。然后给予相同的id给导航图和相应的抽屉菜单项目。

nav_search_new菜单的导航文件:

<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_search_new"
app:startDestination="@id/newSearchFragment">

<fragment
    android:id="@+id/newSearchFragment"
    android:name="com.sequence.main.ui.search.NewSearchFragment"
    android:label="fragment_new_search"
    tools:layout="@layout/fragment_new_search" />
</navigation>

字符串

菜单:

<?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/nav_home_new"
    android:title="@string/home"
    android:icon="@drawable/baseline_home_24"/>

<item android:id="@+id/nav_topic_new"
    android:title="@string/topic"
    android:icon="@drawable/baseline_topic_24"/>

<item android:id="@+id/nav_search_new"
    android:title="@string/search"
    android:icon="@drawable/baseline_search_24"/>
</menu>


最后将所有图添加到主图文件中,并在navHostFragment中将其设置为navGraph:

<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/nav_home_new">
   <include app:graph="@navigation/nav_home_new" />
   <include app:graph="@navigation/nav_topic_new" />
   <include app:graph="@navigation/nav_search_new" />
</navigation>

相关问题