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

时间:2019-04-19 12:51:55

标签: java android navigation android-jetpack

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

编辑:这是我的navigation.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));

我在logcat中收到此错误:

    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)

1 个答案:

答案 0 :(得分:0)

您需要调用action.setTitle(...)而不是直接执行。

相关问题