安卓导航组件 : Pass value (arguments) in fragments

标签 android android-fragments android-architecture-navigation

我做了什么:

我创建了Navigation Drawer Activity,更新了Navigation Drawer Activity的新格式,根据新的Android架构,我用 Navigation Component得到了它结构。

NavigationView代码 NavControllerNavigationUI下面是当我点击任何导航项时打开的 fragment 。

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);

这是给 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" />

导航正在使用这个 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>

我想做什么:

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

场景:我有两个 fragment PrivacyPolicyFragmentTermsConditionsFragment ,在这两个 fragment 中,我只是相应地在 WebView 中打开链接。所以当我点击 Privacy Policy 的菜单项时,我将传递一个与此相关的链接。

在这个新结构中navigation/mobile_navigation.xml打开 fragment ,如何传递参数?

有什么帮助吗?

最佳答案

简单快速的解决方案:

在目标之间传递参数

Bundle bundle = new Bundle();
bundle.putString("amount", amount);
Navigation.findNavController(view).navigate(R.id.confirmationAction, bundle);

和接收

TextView tv = view.findViewById(R.id.textViewAmount);
tv.setText(getArguments().getString("amount"));

关于安卓导航组件 : Pass value (arguments) in fragments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59452243/

相关文章:

android - getFragmentById 返回 null

java - 如何使用 swipeRefreshLayout 更新 fragment 中的新 json 数据

android - 如何传递中间目的地的参数,这些参数是在启动带有导航组件的深度链接时为返回堆栈重新创建的?

android-architecture-components - View.findNavController() 与 Fragment.findNavController()

Android addToBackStack 关闭应用

java - JSONObject 的 names() 方法返回不一致的结果

Android Intent.Action_view 文件权限被拒绝

java - Mapbox 无法在 fragment 中工作

android - 当应用程序不在前台时,NavDeepLinkBuilder 目标被忽略

从商店打开时,Android 应用强制关闭