简体   繁体   中英

Default argument value for action in Jetpack Navigation

Is it possible to set a default argument value for navigation action using Jetpack Navigation?

I have one fragment which requires argument. I want this argument's value to be "1" when using navController.navigate(R.id.action1) and "2" when using navController.navigate(R.id.action2) .

<fragment
    android:id="@+id/myFragment"
    android:name="MyFragment">

    <argument android:name="action_number" />

</fragment>

<!-- action_number should be set to 1 -->
<action
    android:id="@+id/action1"
    app:destination="@id/myFragment" />

<!-- action_number should be set to 2 -->
<action
    android:id="@+id/action2"
    app:destination="@id/myFragment" />

Use Safe Args to pass data with type safety

To add Safe Args to your project, include the following classpath in your top level build.gradle file:

buildscript {
    repositories {
        google()
    }
    dependencies {
        def nav_version = "2.3.0"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
    }
}

To generate Java language code suitable for Java or mixed Java and Kotlin modules, add this line to your app or module's build.gradle file:

apply plugin: "androidx.navigation.safeargs"

Pass argumensts Kotlin :

override fun onClick(v: View) {
   val amountTv: EditText = view!!.findViewById(R.id.editTextAmount)
   val amount = amountTv.text.toString().toInt()
   val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
   v.findNavController().navigate(action)
}

Java:

@Override
public void onClick(View view) {
   EditText amountTv = (EditText) getView().findViewById(R.id.editTextAmount);
   int amount = Integer.parseInt(amountTv.getText().toString());
   ConfirmationAction action =
           SpecifyAmountFragmentDirections.confirmationAction()
   action.setAmount(amount)
   Navigation.findNavController(view).navigate(action);
}

In your receiving destination's code, use the getArguments() method to retrieve the bundle and use its contents

Kotlin:

val args: ConfirmationFragmentArgs by navArgs()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val tv: TextView = view.findViewById(R.id.textViewAmount)
    val amount = args.amount
    tv.text = amount.toString()
}

Java:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    TextView tv = view.findViewById(R.id.textViewAmount);
    int amount = ConfirmationFragmentArgs.fromBundle(getArguments()).getAmount();
    tv.setText(amount + "")
}

For more details: Pass data between destinations

You can use <argument> tag to provide a default value inside of the <action> tag.

For example, your first action would become:

    <action
        android:id="@+id/action1"
        app:destination="@id/myFragment">
        <argument
            android:name="action_number"
            android:defaultValue="1"/>
    </action>

Use default value in argument,

For example:-

<action
    android:id="@+id/action1"
    app:destination="@id/myFragment">
    <argument
        android:name="value"
        android:defaultValue="1"/>
</action>ode here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM