I'm trying to achieve a simple design. One activity with a host fragment.

The thing is, one of the destinations has a bottom navigation bar.

Here's a paint sketch enter image description here

经过一点研究,我发现最好的做法是使用一个宿主片段进行单个活动.

在我的特殊情况下,底部的导航栏不应该在登录和注册片段中可见,只是隐藏它对我来说似乎不合适.

I managed to create an activity with a bottom navigation bar connecting main fragment to frag 1, frag 2 or frag 3, but now I need to add the login fragment and register fragment and I'm not sure how to handle the navigation.

Here's the code for the app without the authentication fragments.

<?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>

也:

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
}

And the navigation file:

<?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>

我应该把导航栏隐藏在碎片上不应该出现的地方吗?

Also, I'm thinking another solution could be to not use the automatic navigation provided from the bottom navigation bar and create my own actions to navigate from main fragment to frag 1, frag 2 or frag 3

推荐答案

Imho,让你的单一活动成为真相的来源,让哪个片段显示和隐藏导航栏,听起来不错.

Kotlin相关问答推荐

KMP:未能添加cafe.adriel.voyager依赖项

Kotlin Coroutine()是如何工作的?S阻止了.

Kotlin-elvis算子don';不使用map.get()

禁用 Gradle执行消息

在 Kotlin 中将 Array 转换为 IntArray 时丢失值

为什么Kotlin有次构造函数和init块?

如何避免键盘打开时jetpack compose 内容上升

Jetpack Compose:当状态从另一个活动改变时强制重组

添加 Kapt 插件后 - 执行 org.jetbrains.kotlin.gradle.internal.KaptExecution 时发生故障

Android 导航组件 - 向上导航打开相同的片段

runBlocking 中的 deferred.await() 抛出的异常即使在被捕获后也被视为未处理

包括登录Elvis operator?

Foo::class.java 和 Foo::javaClass 有什么区别?

在Kotlin中不带类直接引用枚举实例

是否在Kotlin中重写enum toString()?

Android EditText 协程go 抖操作符,如 RxJava

如何在特定条件下清除jetpack数据存储数据

Kotlin:相互递归函数的尾部递归

比较Kotlin的NaN

如何在 Fragment 中使用 Anko DSL?