I am rewriting my simple UI app to use Navigation architecture component, I need to pass a Pojo that implements Parcelable, have not seen any doc on how to do that.
Any help would be appreciated.
What does the Safe Args Gradle plugin do? Select all that apply. Choose as many answers as you see fit. Generates simple object and builder classes for type-safe access to arguments specified for destinations and actions.
In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears. Type a name in the File name field, such as "nav_graph". Select Navigation from the Resource type drop-down list, and then click OK.
Since safe-args-gradle-plugin:1.0.0-alpha03you can use Parcelableobjects by using their fully qualified class name: <argument android:name="item" app:argType="com.example.app.model.Item"/> Parcelable arguments are now supported, using a fully qualified class name for app:type.
Create a new Class User.kt we will use data of custom generic “ User ” having a name, email, password to pass to another fragment. In the dialog write file name as nav_graph and choose Resource type as “ Navigation “. and click Ok. Now open the just created nav_graph.xml file click on the new destination icon and choose both of the fragments.
Show activity on this post. Objects can be passed among fragment and Activities by making the model class Serializable or Parcelable. Parcelable is an Android class and can support more complex serialization of classes.
But Navigation has something even better: SafeArgs. SafeArgs is a gradle plugin that allows you to enter information into the navigation graph about the arguments that you want to pass.
Since safe-args-gradle-plugin:1.0.0-alpha03
you can use Parcelable
objects by using their fully qualified class name:
<argument android:name="item" app:argType="com.example.app.model.Item"/>
Parcelable arguments are now supported, using a fully qualified class name for app:type. The only default value supported is "@null" (https://issuetracker.google.com/issues/79563966)
Source: https://developer.android.com/jetpack/docs/release-notes
To support nullability one has to use android:defaultValue="@null"
with app:nullable="true"
.
I know the answer is already there but this may help someone. Code snippet
In build.gradle add this dependancy
ext{ ... navigation_version = '1.0.0-alpha11' } dependencies { ... classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$navigation_version" }
In app/build.gradle
apply plugin: 'androidx.navigation.safeargs' ...
In Navigation graph
<fragment android:id="@+id/source_fragment_id" android:name="app.test.SourceFragment" android:label="@string/source_fragment_label" tools:layout="@layout/source_fragment_layout"> <action android:id="@+id/action_source_fragment_to_destination_fragment" app:destination="@id/destination_fragment_id" ... /> </fragment> <fragment android:id="@+id/destination_fragment_id" android:name="app.test.DestinationFragment" android:label="@string/destination_fragment_label" tools:layout="@layout/destination_fragment_layout"> <argument android:name="variableName" app:argType="app.test.data.model.CustomModel" /> ... </fragment>
Note: CustomModel should be Parcelable or Serializable.
When navigating to this DestinationFragment from SourceFragment
val direction = SourceFragmentDirections.ActionSourceFragmentToDestinationFragment(customModel) findNavController().navigate(direction)
Now retrieving the value from bundle in DestinationFragment
... import app.test.DestinationFragmentArgs.fromBundle class DestinationFragment : Fragment() { val variableName by lazy { fromBundle(arguments!!).variableName } ... override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { Log.e(DESTINATION_FRAGMENT_TAG,"onCreateView") //Can use CustomModel variable here i.e. variableName } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With