[英]Fragments overlapping in Navigation Drawer Activity project when trying to display a fragment that isn't in the menu
I created a simple project using the "Navigation Drawer Activity" template.我使用“导航抽屉活动”模板创建了一个简单的项目。
When I try to navigate to a fragment that isn't in the navigation menu the fragment appears on top of the current fragment.当我尝试导航到不在导航菜单中的片段时,片段出现在当前片段的顶部。
I am using this in order to navigate to the fragment that isn't in the navigation menu I use this:我使用它是为了导航到不在导航菜单中的片段我使用它:
FragmentManager fm = getActivity().getSupportFragmentManager();
fm.beginTransaction().replace(R.id.nav_host_fragment, nextFrag).commit();
All the app_bar_main and content_main xml files are exactly the same as have been generated by android studio.所有 app_bar_main 和 content_main xml 文件与 android studio 生成的文件完全相同。
I use "nav_host_fragment" above because in my content_main file I have:我使用上面的“nav_host_fragment”,因为在我的 content_main 文件中我有:
<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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<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:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried wrapping another container around it and using that, but nothing changed.我尝试在它周围包裹另一个容器并使用它,但没有任何改变。
You should never be using a FragmentTransaction
when using a NavHostFragment
and the Navigation Component (in fact, even when you aren't using those, you cannot ever replace a <fragment>
added fragment.在使用NavHostFragment
和导航组件时,您永远不应该使用FragmentTransaction
(事实上,即使您不使用它们,也永远无法替换<fragment>
添加的片段。
Instead, you should follow the Tie destinations to menu items and add your fragment to your res/navigation/mobile_navigation.xml
file and make sure that the ID of your menu.xml
file item matches the android:id
of the destination in your graph. Instead, you should follow the Tie destinations to menu items and add your fragment to your res/navigation/mobile_navigation.xml
file and make sure that the ID of your menu.xml
file item matches the android:id
of the destination in your graph.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.