Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass object of type Parcelable to a Fragment using Navigation type safeargs plugin

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.

like image 761
Jerry Okafor Avatar asked Jun 03 '18 08:06

Jerry Okafor


People also ask

What does the safe args gradle plugin do select all that apply?

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.

How do you enable your project to use navigation components?

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.

How to use parcelableobjects in safe-ARGs-Gradle-plugin?

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.

How to pass user data from one fragment to another fragment?

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.

How to pass objects between fragment and activities in Android?

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.

What is safeargs in Android navigation?

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.


2 Answers

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".

like image 182
mbo Avatar answered Oct 06 '22 11:10

mbo


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      }  } 
like image 37
Suyash Chavan Avatar answered Oct 06 '22 12:10

Suyash Chavan