如何在不发送参数的情况下从一个片段导航到另一个片段



我目前正在研究一种使用导航组件中的 SafeArgs 将参数从一个片段发送到另一个片段的方法。我已经按照几篇博客文章中的描述在目标片段中设置了参数,但我也希望能够在没有参数的情况下导航到该目标。我不想传递一堆空值和 0。

编辑:这是我的导航.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/nav_graph"
    android:label="@string/app_name"
    app:startDestination="@+id/homeFragment">
    <fragment
        android:id="@+id/homeFragment"
        android:name="com.andreramon.todo.destinations.HomeFragment"
        android:label=" "
        tools:layout="@layout/fragment_home">
        <action
            android:id="@+id/action_homeFragment_to_imprintFragment"
            app:destination="@id/imprintFragment" />
        <action
            android:id="@+id/action_homeFragment_to_settingsFragment"
            app:destination="@id/settingsFragment" />
        <action
            android:id="@+id/action_homeFragment_to_addEditTaskFragment"
            app:destination="@id/addEditTaskFragment"/>
    </fragment>
    <fragment
        android:id="@+id/addEditTaskFragment"
    android:name="com.andreramon.todo.destinations.AddEditTaskFragment"
        android:label="@string/toolbar_parent_tasks"
        tools:layout="@layout/fragment_add_edit_task">
        <argument
            android:name="id"
            app:argType="integer"/>
        <argument
            android:name="title"
            app:argType="string" />
        <argument
            android:name="priority"
            app:argType="integer" />
        <argument
            android:name="duedate"
            app:argType="string" />
        <argument
            android:name="remindme"
            app:argType="string" />
        <argument
            android:name="addanote"
            app:argType="string" />
    </fragment>
    <fragment
        android:id="@+id/imprintFragment"
        android:name="com.andreramon.todo.destinations.ImprintFragment"
        android:label="@string/drawer_imprint"
        tools:layout="@layout/fragment_imprint" />
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.andreramon.todo.destinations.SettingsFragment"
        android:label="@string/drawer_settings"
        tools:layout="@layout/fragment_settings" />
</navigation>

这就是我目前使用参数导航到目的地的方式:

NavDirections action = HomeFragmentDirections.actionHomeFragmentToAddEditTaskFragment(...)

我使用以下方法检索目标处的参数:

titleEditText.setText(AddEditTaskFragmentArgs.fromBundle(getArguments()).getTitle());

等。

现在,当尝试使用以下方法导航到AddEditTaskFragment时:

addTaskFab.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.addEditTaskFragment));

我在日志猫中收到此错误:

    Process: com.andreramon.todo, PID: 10014
    java.lang.IllegalArgumentException: Required argument "id" is missing and does not have an android:defaultValue
        at com.andreramon.todo.destinations.AddEditTaskFragmentArgs.fromBundle(AddEditTaskFragmentArgs.java:33)
        at com.andreramon.todo.destinations.AddEditTaskFragment.onViewCreated(AddEditTaskFragment.java:71)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:895)
        at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2092)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1822)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1723)
        at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManagerImpl.java:146)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

你需要调用action.setTitle(...(,而不是直接调用。

相关内容

  • 没有找到相关文章

最新更新