Navigation Components: Passing arguments from global actions results in NullPointerException

Josip Domazet :

Library in use: Navigation Components (from android itself)

I am trying to pass arguments (with safe-args) to an activity via a global action. The activity is launched by clicking on an item in a navigation drawer (hence its declaration as global action). Because of that I cannot simply pass my arguments from the other class.

The launch is successful but when trying to retrieve the arguments I get a NullPointerException.

nav_graph.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"
    app:startDestination="...">
...

   <activity
        android:id="@+id/myAct"
        android:name="com.example.client.MyActivity"
        android:label="MyActivity"
        tools:layout="@layout/activity_my">
        <argument
            android:name="util_bool"
            android:defaultValue="false"
            app:argType="boolean" />
    </activity>

    <!--Global action-->

    <action
        android:id="@+id/launch_util"
        app:destination="@id/myAct">
        <argument
            android:name="util_bool"
            android:defaultValue="true"
            app:argType="boolean" />
    </action>
</navigation>

MyActivity.java

  @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_barcode);
        // This results in the NullPointerException:
        boolean utilBool = NavGraphDirections.launchUtil().getUtilBool();

    }

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">
    <item android:title="....">
        <menu>
            <group android:checkableBehavior="single">

                <item
                    android:id="@+id/myAct"
                    android:icon="@drawable/start_icon"
                    android:menuCategory="secondary"
                    android:checkable="false"
                    android:title="Beginne" />

                ...
            </group>
        </menu>
    </item>

    <item android:title="....">
        <menu>
            <item
                ...
        </menu>
    </item>

</menu>
ianhanniballake :

The Directions classes are for use with navigate(). You want use the Args class that is generated for each destination to retrieve the arguments:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_barcode);
    // Extract the arguments from the Intent's extras
    boolean utilBool = MyActivityArgs.fromBundle(getIntent().getExtras()).getUtilBool();

}

Guess you like

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