Android 导航组件:在片段中传递值(参数)

Pra*_*ani 24 android android-fragments android-architecture-navigation

我做了什么:

我创建了抽屉式导航活动,作为更新的新格式抽屉式导航活动,按照新的Android架构,我得到了它的导航组件结构。

当我单击任何导航项时,下面NavigationView带有NavController和的代码NavigationUI正在打开片段。

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_profile, R.id.nav_privacy_policy,
            R.id.nav_terms, R.id.nav_contact_us, R.id.nav_share, R.id.nav_send)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
Run Code Online (Sandbox Code Playgroud)

这是为了nav_host_fragment

<fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
Run Code Online (Sandbox Code Playgroud)

使用这个进行导航 navigation/mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/nav_home">

    <fragment
        android:id="@+id/nav_home"
        android:name="com.sohamerp.marsremedies.fragment.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/nav_profile"
        android:name="com.sohamerp.marsremedies.fragment.ProfileFragment"
        android:label="@string/menu_my_profile"
        tools:layout="@layout/fragment_profile" />

    <fragment
        android:id="@+id/nav_privacy_policy"
        android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
        android:label="@string/menu_privacy_policy"
        tools:layout="@layout/fragment_privacy_policy" />

    <fragment
        android:id="@+id/nav_terms"
        android:name="com.sohamerp.marsremedies.fragment.TermsConditionFragment"
        android:label="@string/menu_terms"
        tools:layout="@layout/fragment_terms_condition" />

    <fragment
        android:id="@+id/nav_contact_us"
        android:name="com.sohamerp.marsremedies.fragment.ContactUsFragment"
        android:label="@string/menu_contact_us"
        tools:layout="@layout/fragment_terms_condition" />

</navigation>
Run Code Online (Sandbox Code Playgroud)

我想做的事:

现在我想在调用 Fragment 时将一些值作为包(参数)传递给它。

场景:我有两个片段PrivacyPolicyFragmentTermsConditionsFragment,在这两个片段,我只是打开相应的内部的WebView链接。所以当我点击 的菜单项时Privacy Policy,我会传递一个相关的链接。

在这个新的结构navigation/mobile_navigation.xml开放片段中,我如何传递参数?

有什么帮助吗?

小智 40

简单快速的解决方案:

在目的地之间传递参数

Bundle bundle = new Bundle();
bundle.putString("amount", amount);
Navigation.findNavController(view).navigate(R.id.confirmationAction, bundle);
Run Code Online (Sandbox Code Playgroud)

并接收

TextView tv = view.findViewById(R.id.textViewAmount);
tv.setText(getArguments().getString("amount"));
Run Code Online (Sandbox Code Playgroud)


Pra*_*ani 19

所以我忘了通过这个链接:定义目的地参数

但是这个答案对像我这样的所有懒惰的人都有帮助:

在项目级build.gradle 中添加依赖

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0"
Run Code Online (Sandbox Code Playgroud)

在应用级build.gradle 中应用插件:

apply plugin: "androidx.navigation.safeargs"
Run Code Online (Sandbox Code Playgroud)

使用 XML:预定义(静态)值:

在导航/navigation/mobile_navigation.xml 的xml 文件中声明argument如下标签,或者您可以通过此链接进行设计:

<fragment
    android:id="@+id/nav_privacy_policy"
    android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
    android:label="@string/menu_privacy_policy"
    tools:layout="@layout/fragment_privacy_policy" >
    <argument
        android:name="privacyPolicyLink"
        app:argType="string"
        android:defaultValue="http://sohamerp.com/avo/avo_privacy_policy.html"/>
</fragment>

<fragment
    android:id="@+id/nav_terms"
    android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
    android:label="@string/menu_terms"
    tools:layout="@layout/fragment_terms_condition" >
    <argument
        android:name="privacyPolicyLink"
        app:argType="string"
        android:defaultValue="http://sohamerp.com/avo/avo_privacy_policy.html"/>
</fragment>
Run Code Online (Sandbox Code Playgroud)

现在您必须在 Fragment 中编写代码,例如:

if(getArguments() != null) {
    // The getPrivacyPolicyLink() method will be created automatically.
    String url = PrivacyPolicyFragmentArgs.fromBundle(getArguments()).getPrivacyPolicyLink();
}
Run Code Online (Sandbox Code Playgroud)

希望它会帮助你其他人。

  • 你如何传递数据?就像从这里开始:`Navigation.findNavController(view).navigate(R.id.action);` (5认同)

Viv*_*nde 15

在这种情况下,您可以使用

  private NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
  // Create the Bundle to pass, you can put String, Integer, or serializable object
  Bundle bundle = new Bundle();
  bundle.putString("link","http://yourlink.com/policy");
  bundle.putSerializable("USER", user); // Serializable Object
  navController.navigate(R.id.nav_terms, bundle); // called fragment with agruments
Run Code Online (Sandbox Code Playgroud)

如果有任何帮助可以回复

  • 在下一个片段中,在 onCreateView 中,调用 Bundle arguments = getArguments(); if (参数!= null) { } (4认同)
  • 如何在 R.id.nav_terms 中获取捆绑包? (3认同)

May*_*kun 7

要将参数传递给其他片段/目标,请使用确保类型安全的安全参数。就像@bromden 所示,Safe Args 将为每个片段/目的地生成一个类action。然后,您可以将参数传递到action导航到 Fragments 的 。

在接收片段中,假设PrivacyFragment您的代码在 Kotlin 中,则使用navArgs()属性委托来访问参数。IE

val args: PrivacyFragmentArgs by navArgs()
Run Code Online (Sandbox Code Playgroud)

为了更好地理解这一点,请访问目的地之间的通行证数据