ShoaibuldinMemon :
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>
Alexey Denysenko :
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"
.