How to navigate from one fragment to another without sending arguments

André Ramon :

I am currently working on a way to send arguments from one fragment to another using SafeArgs from the Navigation Component. I have set the arguments in the destination fragment as described in several blog posts, but I also want to be able to navigate to that destination without arguments. I don't want to pass a bunch of nulls and 0s.

EDIT: Here is my 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>

This is how I currently navigate to the destination using arguments:

NavDirections action = HomeFragmentDirections.actionHomeFragmentToAddEditTaskFragment(...)

I retrieve the arguments at the destination using:

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

etc.

Now, when trying to navigate to AddEditTaskFragment using:

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

I get this error in the 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)
André Ramon :

You need to call action.setTitle(...) instead of doing it directly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=166689&siteId=1