[英]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.