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.
经过一点研究,我发现最好的做法是使用一个宿主片段进行单个活动.
在我的特殊情况下,底部的导航栏不应该在登录和注册片段中可见,只是隐藏它对我来说似乎不合适.
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