Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle is getting null by using safe Args navigation

I'm trying to use New Navigation functionality of android x by using java and i am facing the below issue

I am using Safe Args to pass the data from one destination from other but my bundle is always null. i have tried so many options.

Tried argument pass without Safe Args and even with Safe Args i am getting error

// Passing an argument in fragment
nameBtn.setOnClickListener(v -> {
            AccountFragmentDirections.ToNameFragment direction =
                    AccountFragmentDirections.toNameFragment().setNameArgument(editName.getText().toString());
            Navigation.findNavController(v).navigate(direction);
        });


//Retriving the nameArgument from bundle
        String name  = NameFragmentArgs.fromBundle(savedInstanceState).getNameArgument();


<!-- Fragments in Navigation graph -->
    <fragment
        android:id="@+id/nameFragment"
        android:name="com.example.navigationdemo.NameFragment"
        android:label="fragment_name"
        tools:layout="@layout/fragment_name">

        <argument
            android:name="nameArgument"
            app:argType="string"
            app:nullable="true"
            android:defaultValue="none" />
    </fragment>

    <fragment
        android:id="@+id/accountFragment"
        android:name="com.example.navigationdemo.AccountFragment"
        android:label="fragment_account"
        tools:layout="@layout/fragment_account" >
        <action
            android:id="@+id/toNameFragment"
            app:destination="@id/nameFragment" />
    </fragment>
like image 779
ShoaibuldinMemon Avatar asked Nov 02 '25 17:11

ShoaibuldinMemon


1 Answers

You shouldn't use savedInstanceState to retrieve safe args. Instead of

String name  = NameFragmentArgs.fromBundle(savedInstanceState).getNameArgument();

Use

String name  = NameFragmentArgs.fromBundle(getArguments).getNameArgument();

Also, please pay attention that android:defaultValue="none" will be literally String with value "none".

like image 189
Alexey Denysenko Avatar answered Nov 05 '25 08:11

Alexey Denysenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!