image-20200926195332267.png
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/HomeFragment">
<fragment
//下一步中方法一对应的fragment的ID
android:id="@+id/HomeFragment"
android:name="com.cy.navigationtest.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
//下一步中方法二对应的action的ID
<action
android:id="@+id/next_action"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="com.cy.navigationtest.DetailFragment"
android:label="fragment_detail"
tools:layout="@layout/fragment_detail" />
</navigation>
- 在主
fragment中,在对应场景下进行跳转。有两种跳转方式,代码如下:
-
方式一:直接跳转到对应fragment,跳转规则可不受
navigation.xml指使,只要xml中有这个fragment的ID就行。findNavController().navigate(R.id.HomeFragment, null) //fragment_one:要跳转的fragment在navigation.xml中指定的ID ; null为bundle,可不传。 -
方式二:按
navigation.xml的规则进行跳转。Navigation.createNavigateOnClickListener(R.id.next_action, null) //next_action为navigation.xml中指定action的ID; 跳转规则就在这个action中
-
方式三:使用 Safe Args Gradle 插件
我也还没搞清楚,具体看官网。

评论列表