我正在尝试使用导航主机片段进行片段事务,但是在导航到目标片段时,旧片段仍然像在彼此上添加的片段一样在屏幕上可见。
这是我的代码:
settings_nav.xml
tail -f error_log|fgrep '[rewrite:'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/settings_nav"
app:startDestination="@+id/WelcomeFragment">
<fragment
android:id="@+id/WelcomeFragment"
android:name="com.example.ui.fragment.WelcomeFragment"
android:label="WelcomeFragment">
<action
android:id="@+id/action_WelcomeFragment_to_secondFragment"
app:destination="@+id/secondFragment"
/>
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.ui.fragment.ConfigFragment"
android:label="secondFragment">
</fragment>
WelcomeFragment.kt
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ui.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:theme="@style/AppTheme.AppBarOverlay" />
<TextView
android:id="@+id/title_text"
style="@style/SettingsTitleText"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height" />
<fragment
android:id="@+id/all_navigation_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/settings_nav"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"
android:layout_gravity="top"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
MainActivity.kt
button.setOnClickListener{
findNavController().navigate(R.id.action_WelcomeFragment_to_secondFragment)
}
为什么要互相覆盖?我想念任何明显的东西吗?