【问题标题】:Android Navigation Component - Why add arguments in the navigation graph?Android 导航组件 - 为什么要在导航图中添加参数?
【发布时间】:2020-06-15 19:19:57
【问题描述】:

我一直在关注 CodingWithMitch 的 tutorial(和 github code)导航组件,但使用的是 Java 而不是 Kotlin。然而,这并没有带来任何问题。

我的问题是,不使用 Safe Args,在 nav_graph.xml 中添加参数有什么意义。

在本例中,SpecifyAmountFragment 需要来自前一个 Fragment 的 String 参数,称为 ChooseRecipientFragment

nav_graph.xml的设计视图:

来自nav_graph.xml的代码sn-p:

<fragment
        android:id="@+id/specifyAmountFragment"
        android:name="com.asfartz.navigation_component_basics.SpecifyAmountFragment"
        android:label="fragment_specify_amount"
        tools:layout="@layout/fragment_specify_amount">

        <argument android:name="recipient"
            app:argType="string" />

        <action
            android:id="@+id/action_specifyAmountFragment_to_confirmationFragment"
            app:destination="@id/confirmationFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/mainFragment"
            app:popUpToInclusive="false" />
    </fragment>

Java 代码:

public class SpecifyAmountFragment extends Fragment {

    private NavController navController;
    private Button bSend, bCancel;
    private String recipient;
    private TextView tvRecipient;
    private TextInputEditText inputAmount;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_specify_amount, container, false);
        bSend = view.findViewById(R.id.send_btn);
        bCancel = view.findViewById(R.id.cancel_btn);
        tvRecipient = view.findViewById(R.id.recipient);
        inputAmount = view.findViewById(R.id.input_amount);

        recipient = getArguments().getString("recipient");

        String messagge = "Sending money to " + recipient;
        tvRecipient.setText(messagge);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        navController = Navigation.findNavController(view);

        bSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(inputAmount.getText())) {
                    Money money = new Money(new BigDecimal(inputAmount.getText().toString()));
                    Bundle b = new Bundle();
                    b.putString("recipient", recipient);
                    b.putParcelable("money", money);

                    navController.navigate(R.id.action_specifyAmountFragment_to_confirmationFragment, b);
                } else {
                    Toast.makeText(getActivity(), "Enter an amount", Toast.LENGTH_SHORT).show();
                }
            }
        });

        bCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity() != null) {
                    getActivity().onBackPressed();
                } else {
                    Toast.makeText(getContext(), "Activity is null", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

我在 OnCreateView 中接收包,从中获取我在 nav_graph.xml 中声明的所需字符串参数。稍后,我将在另一个包中(在 bSend.setOnClickListener(...) 中)发送这些数据。

如果我注释掉所有 &lt;argument&gt; 标记并再次运行该应用程序,该应用程序仍将正常运行(从一个片段接收数据并将其传递给另一个片段)。没有验证,所以为什么要添加这些标签,除了为了清楚起见?

【问题讨论】:

    标签: java android android-architecture-navigation android-navigation-graph


    【解决方案1】:

    当使用导航架构组件时,你不应该使用 recipient = getArguments().getString("recipient"); 相反,您应该使用生成的目标类,它应该是SpecifyAmountFragmentArgs,并以这种方式获取数据: SpecifyAmountFragment.fromBundle(Pass arguments here).getRecipient 将提供类型安全。

    【讨论】:

    • 我相信您指的是 SafeArgs 插件,它为每个目的地生成这些类 (ConfirmationFragmentArgs)。但我问.xml 中的&lt;argument&gt; 部分在没有SafeArgs 的情况下是否有任何用处
    • 你好在这里传递什么:(在这里传递参数)你能详细解释一下@SamirSpahic
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多