简体   繁体   English

Jetpack Navigation 中操作的默认参数值

[英]Default argument value for action in Jetpack Navigation

Is it possible to set a default argument value for navigation action using Jetpack Navigation?是否可以使用 Jetpack Navigation 为导航操作设置默认参数值?

I have one fragment which requires argument.我有一个需要论证的fragment 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) .我希望在使用 navController.navigate(R.id.action1) 时此参数的值为"1" ,在使用navController.navigate(R.id.action1) navController.navigate(R.id.action2) "2"

<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使用 Safe Args 传递具有类型安全性的数据

To add Safe Args to your project, include the following classpath in your top level build.gradle file:要将 Safe Args 添加到您的项目中,请在顶级 build.gradle 文件中包含以下类路径:

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: 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 :通过参数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: 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在接收目的地的代码中,使用getArguments()方法检索包并使用其内容

Kotlin: 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: 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.您可以使用<argument>标记在<action>标记内提供默认值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack导航的action有什么用? - What is the use of the action of Jetpack navigation? 导航操作未在 Jetpack Navigation 上执行 - Nav action not executing on Jetpack Navigation Jetpack Compose Navigation - 将参数传递给 startDestination - Jetpack Compose Navigation - pass argument to startDestination 将参数传递给 Jetpack Compose 中的嵌套导航图 - Pass an argument to a nested navigation graph in Jetpack Compose Android Jetpack 导航 - 带抽屉项的自定义操作 - Android Jetpack Navigation - Custom Action with Drawer Item 如何删除 Jetpack Compose Navigation 中的默认过渡 - How to remove default transitions in Jetpack Compose Navigation 未解决的参考:当使用自定义 Parcelable 参数时:Jetpack Navigation - Unresolved Reference: When Custom Parcelable Argument Used : Jetpack Navigation 当参数具有默认值时,为什么我不能使用导航组件将参数传递给片段? - Why can't I pass an argument to fragment using navigation component when that argument has a default value? 使用jetpack导航在操作栏中显示向后欠款 - Use jetpack navigation to display back arrrow in the action bar AndroidX JetPack NavigationUI - 覆盖抽屉中非导航项的操作 - AndroidX JetPack NavigationUI - Overriding Action on Non-Navigation Item In Drawer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM