Android 导航抽屉将片段替换为另一个有问题的保留抽屉选项

Android 导航抽屉将片段替换为另一个有问题的保留抽屉选项,android,xml,android-layout,android-fragments,kotlin,Android,Xml,Android Layout,Android Fragments,Kotlin,我有一个基本的应用程序,包含一个抽屉布局。抽屉中的每个选项都会打开一个新的片段页面供用户查看。这是抽屉布局代码 <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://schem

我有一个基本的应用程序,包含一个抽屉布局。抽屉中的每个选项都会打开一个新的片段页面供用户查看。这是抽屉布局代码

<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:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>
根据我的理解,当您单击抽屉布局中的按钮时,主机片段中的片段将替换为相关的单击片段

我的问题是。。。我如何通过编程将另一个片段放在这个nav主机视图中,该视图尚未设置抽屉布局。我的问题是其中一个片段包含一个用户的配置文件,其中包含一个编辑按钮。我想在单击编辑按钮时打开一个新片段,并希望用户在不想进入其他活动时仍然可以访问抽屉

在我看来,我只想用edit_片段替换profile_片段。但是当我试着在下面做一些类似的事情时,它会把片段放在profile one的上面,这样你就会看到一个奇怪的屏幕,上面有两个片段

val frag: Fragment = EditYourEmissionsFragment()
val fragMan:FragmentManager = activity!!.supportFragmentManager
val fragTran: FragmentTransaction = fragMan.beginTransaction()
fragTran.add(R.id.nav_host_fragment, frag)
fragTran.addToBackStack(null)
fragTran.commit()
对于Android和Kotlin,我还是一个相对的新手,所以我很难理解我将如何处理我的用户案例

尝试帮助描绘我希望发生的事情的场景:

用户加载应用程序并登陆home_碎片。 打开抽屉菜单,单击“配置文件”\u片段。 一旦进入profile_片段打开,用户按edit。 一个新的片段接管了屏幕,但保留了打开菜单抽屉的功能。 用户现在可以打开抽屉菜单并导航到应用程序中的其他位置。或者保存他们的个人资料并返回到个人资料片段。
提前感谢您的帮助。如果不是特别清楚,请道歉。

在导航图中包括EditYourEmissionsFragment,如下所示:

    <fragment
        android:id="@+id/EditYourEmissionsFragment"
        android:name="com.example.application.EditYourEmissionsFragment"
        android:label="Edit Profile"
        tools:layout="@layout/fragment_edit_omissions"/>
在你的主要活动中

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.my_nav_host)
        return navController.navigateUp(myAppBarConfiguration) || super.onSupportNavigateUp()
    }

在导航图中包括EditYourEmissionsFragment,如下所示:

    <fragment
        android:id="@+id/EditYourEmissionsFragment"
        android:name="com.example.application.EditYourEmissionsFragment"
        android:label="Edit Profile"
        tools:layout="@layout/fragment_edit_omissions"/>
在你的主要活动中

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.my_nav_host)
        return navController.navigateUp(myAppBarConfiguration) || super.onSupportNavigateUp()
    }

您可以使用childFragmentManager在当前片段内托管片段。更多信息您可以使用childFragmentManager在当前片段中托管片段。更多信息嗨,埃文斯,我尝试过这个方法。它在一定程度上起作用,就像导航到编辑页面一样。然而,phones back按钮只是刷新小部件,而不是返回到profile片段。因此,不确定如何修复此位。在MainActivity上,添加override on Backpressed函数,并在其中添加什么?由于仅仅添加该方法不会改变任何事情Hi Evans,我已经尝试过这种方法。它在一定程度上起作用,就像导航到编辑页面一样。然而,phones back按钮只是刷新小部件,而不是返回到profile片段。因此,不确定如何修复此位。在MainActivity上,添加override on Backpressed函数,并在其中添加什么?因为仅仅添加该方法不会改变任何事情
    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.my_nav_host)
        return navController.navigateUp(myAppBarConfiguration) || super.onSupportNavigateUp()
    }