无法使用jetpack导航导航到片段?

时间:2019-07-18 17:15:00

标签: android android-architecture-navigation

我想从HomeFragment导航到DetailFragment 在RecyclerView项目中,单击“我正在调用DetailFragment”,然后 它不起作用,我看到它进入了onClickListener,但它没有走得更远,也没有调用DetailFragment

viewBinding.assetImage.setOnClickListener {
        val bundle = bundleOf("owner" to "TestUser")
        Navigation.createNavigateOnClickListener(R.id.detail_screen,bundle)
      }

<fragment
    android:id="@+id/home_screen"
    android:name="HomeFragment"
    android:label="@string/title_home"
    tools:layout="@layout/fragment_home">
    <action
        android:id="@+id/action_home_screen_to_detailFragment"
        app:destination="@id/detail_screen" />
</fragment>
<fragment
    android:id="@+id/detail_screen"
    android:name="DetailFragment"
    android:label="DetailFragment"
    tools:layout="@layout/fragment_detail">
    <argument
        android:name="owner"
        app:argType="string" />
</fragment>

2 个答案:

答案 0 :(得分:2)

以下是您的解决方案,

   val bundle = Bundle()
   bundle.putString("owner", "TestUser")
   if (it.findNavController().currentDestination?.id == R.id.home_screen) {
              it.findNavController().navigate(R.id.action_home_screen_to_detailFragment, bundle)
   }

答案 1 :(得分:0)

您可以使用safeArgs传递数据。 This可以帮助您

  1. 您转到导航xml文件
  2. 选择要传递值的片段。
  3. 添加属性

picture enter image description here

  1. 构建→重建

  2. 然后您可以像这样使用它 <script src="runtime.dd2d878cd9ae88476a26.js" type="module"></script> <script src="polyfills.8ce2a1027d74b4930bab.js" type="module"></script> <script src="scripts.b7617d15d290ae695226.js" type="module"></script> <script src="main.c6dce3ad2ed82428de43.js" type="module"></script>

相关问题