Jetpack导航无法导航到其他目的地

时间:2019-11-13 19:49:24

标签: android data-binding android-jetpack-navigation

我正在两个不同的活动中使用jetpack导航,每个活动都有其导航主机。我有Main活动,一切正常,而Setup活动有2个片段,一个有一个按钮,单击该按钮应导航到其他片段。

该活动的代码如下:

class SetupActivity : AppCompatActivity() {
    private val navController by lazy { findNavController(R.id.nav_host_fragment) }
    private lateinit var appBarConfig: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        DataBindingUtil.setContentView<ActivitySetupBinding>(this, R.layout.activity_setup)
        setSupportActionBar(toolbar)
        appBarConfig = AppBarConfiguration(navController.graph)
        setupActionBarWithNavController(navController, appBarConfig)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp(appBarConfig) || super.onSupportNavigateUp()
    }
}

下面是活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<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:context=".ui.SetupActivity"
    tools:showIn="@layout/activity_setup">

    <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_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/setup" />
</androidx.constraintlayout.widget.ConstraintLayout>

下面是第一个片段的代码,它也是一个起始目标

class ConfirmClassFragment : Fragment() {
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val binding = FragmentConfirmClassBinding.inflate(inflater, container, false)
        binding.fragment = this

        return binding.root
    }

    fun navigate() {
        toast("clicked") //this get called
        val direction = ConfirmClassFragmentDirections.actionConfirmToSetup()
        findNavController().navigate(direction) //THE PROBLEM IS HERE
        //findNavController().navigate(R.id.setup)
    }
}

我正在使用数据绑定如下调用fun navigate()

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="fragment"
            type="com.nux.ui.fragments.ConfirmClassFragment" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/spacing_middle">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{() -> fragment.navigate()}"
            android:text="@string/continue_" />

    </LinearLayout>
</layout>

以及导航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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/setup"
    app:startDestination="@id/confirm">

    <fragment
        android:id="@+id/setup"
        android:name="com.nux.ui.fragments.SetInfoFragment"
        android:label="@string/setup"
        tools:layout="@layout/fragment_set_info" />
    <fragment
        android:id="@+id/confirm"
        android:name="com.nux.ui.fragments.ConfirmClassFragment"
        android:label="@string/data_confirmation"
        tools:layout="@layout/fragment_confirm_class">
        <action
            android:id="@+id/action_confirm_to_setup"
            app:destination="@id/setup" />
    </fragment>
</navigation>

当我单击按钮时,会调用乐趣,但是什么也没有发生!怎么了?

1 个答案:

答案 0 :(得分:0)

从代码中,我可以看到导航图中的3个元素具有相同的ID。

@ id / setup只能使用一次,请将导航图的名称从setup更改为其他名称。