1、在所要切换的界面上添加一个Button
界面1.
界面2.
2、在Navigation中添加切换关系
android:id="@+id/FirstFragment"
android:name="com.example.baogutou.FirstFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first">
android:id="@+id/action_FirstFragment_to_SecondFragment"
app:destination="@id/SecondFragment" />
android:id="@+id/SecondFragment"
android:name="com.example.baogutou.SecondFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_second">
android:id="@+id/action_SecondFragment_to_FirstFragment"
app:destination="@id/blankFragment" />
3.在界面中监听按钮并执行界面转换
在界面1中
view.findViewById(R.id.button_1).setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
在界面二中
view.findViewById(R.id.button_2).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)