Android Jetpack导航,另一个主机片段中的主机片段

Ghi*_*ard 10 android kotlin android-jetpack

我正在尝试实现一个简单的设计。带有主机片段的一项活动。

问题是,其中一个目的地有一个底部导航栏。

这是油漆素描 在此处输入图片说明

经过一番研究,我发现最佳实践是将单个Activity与宿主片段一起使用。

在我的特定情况下,底部的导航栏在登录和注册片段中应该不可见,而对我而言似乎只是隐藏它。

我设法用底部导航栏创建了一个活动,该导航栏将主片段连接到片段1片段2片段3,但是现在我需要添加登录片段注册片段,而且我不确定如何处理导航。

这是不带身份验证片段的应用程序代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Presentation.MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        app:title="Glucose Entries"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?colorPrimary"
        android:theme="@style/ToolbarTheme"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation_bar"/>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/mobile_navigation"
        app:defaultNavHost="true" />

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

也:

class MainActivity : AppCompatActivity() {

    private lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        navController = Navigation.findNavController(this, R.id.nav_host_fragment)
        bottom_nav.setupWithNavController(navController)
        NavigationUI.setupActionBarWithNavController(this, navController)
    }

    // stuff
}
Run Code Online (Sandbox Code Playgroud)

和导航文件:

<?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/mainListFragment">

    <!--frag 1 -->
    <fragment
        android:id="@+id/mainListFragment"
        android:name="com.gluco.Presentation.MainList.MainListFragment"
        android:label="Glucose Entries"
        tools:layout="@layout/main_list_fragment">
    </fragment>

    <!--frag 2 -->
    <fragment
        android:id="@+id/statisticsFragment"
        android:name="com.gluco.Presentation.Statistics.StatisticsFragment"
        android:label="statistics_fragment"
        tools:layout="@layout/statistics_fragment" />

    <!--frag 3 -->
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.gluco.Presentation.Settings.SettingsFragment"
        android:label="SettingsFragment" />

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

我是否应该将导航栏隐藏在不应存在的片段上?

另外,我在想另一种解决方案可能是不使用底部导航栏提供的自动导航,而是创建自己的操作以从主片段导航到片段1片段2片段3。

Aar*_*man 1

恕我直言,让您的单个活动成为哪个片段显示和隐藏导航栏的事实来源,这对我来说听起来不错。