我对此进行了很多研究,但找不到解决方案,我无法弄清楚出了什么问题。我正在使用导航组件库和单个活动。当用户注销时,它会将用户带到登录,底部导航被删除,它留下一个空白区域,就像整个布局已经向上移动了一点(img 1)。然后当您登录时,设计似乎像以前一样向上移动,但您显示了底部导航视图并显示了应用栏(img 2)。这很奇怪,我只是不知道我在哪里弄错了。应用程序流程是这样的:当用户打开应用程序时,它会将他带到 HomeFragment,如果他已经登录,则访问 homeFragment,否则 homefragment 检查他是否未登录,将他带到登录 Fragment。但是当用户关闭应用程序并重新启动它时显示良好,或者如果用户已经登录并打开应用程序,它会显示得很好。错误发生:当用户注销并进入登录屏幕时,登录后它仍然存在,直到应用重新启动。对不起那个帖子的丑陋:-\
这是在登录屏幕中删除底部栏视图和应用栏的活动代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navigationController = findNavController(R.id.nav_host_fragment)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_nav)
findNavController(R.id.nav_host_fragment).addOnNavigatedListener { _, destination ->
when (destination.id) {
R.id.register1Fragment -> hideBottomNavigation()
R.id.register2Fragment -> hideBottomNavigation()
R.id.loginFragment -> hideBottomNavigation()
R.id.chatLogFragment -> hideBottomNavigation()
else -> showBottomNavigation()
}
}
// This creates a link between the bottomNavigationView and the navigation component (main_navigation_graph.xml)
NavigationUI.setupWithNavController(bottomNavigationView, navigationController)
// Sets up the Toolbar actions (like Back Button) to be managed by the Navigation Component
NavigationUI.setupActionBarWithNavController(this, navigationController)
}
private fun hideBottomNavigation() {
// bottom_navigation is BottomNavigationView
with(bottom_nav) {
if (visibility == View.VISIBLE && alpha == 1f) {
animate()
.alpha(0f)
.withEndAction { visibility = View.GONE }
.duration = 200
}
}
}
private fun showBottomNavigation() {
// bottom_navigation is BottomNavigationView
with(bottom_nav) {
visibility = View.VISIBLE
animate()
.alpha(1f)
.duration = 200
}
}
override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
}
这是 MainActivity 的 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_navigation_graph"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
style="@style/Widget.MaterialComponents.BottomNavigationView.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:labelVisibilityMode="unlabeled"
android:background="@color/colorPrimary"
app:itemBackground="@color/bottom_nav_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/menu_bottom_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
下面是 Login 片段的一个例外代码,他的 Layout 高度和宽度设置为 match_parent:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Remove ActionBar
val fragmentActivity = activity as AppCompatActivity
fragmentActivity.supportActionBar?.hide()
//Remove status bar
fragmentActivity.window.decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
这是 HomeFragment 的一个例外:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
if (FirebaseAuth.getInstance().uid == null) {
NavHostFragment.findNavController(this).navigate(R.id.action_homeFragment_to_loginFragment)
}
}
.....
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity!!.title = "Chapperone"
val fragmentActivity = activity as AppCompatActivity
fragmentActivity.supportActionBar?.show()
fragmentActivity.supportActionBar?.title = "Chapperone"
fragmentActivity.supportActionBar?.setDisplayHomeAsUpEnabled(false)
}
导航图 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/main_navigation_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.gochapperone.Chapperone.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/blank_fragment" >
<action
android:id="@+id/action_homeFragment_to_loginFragment"
app:destination="@id/loginFragment"
app:popUpTo="@id/homeFragment"
app:popUpToInclusive="true" />
<action
android:id="@+id/action_homeFragment_to_editUserProfileFragment"
app:destination="@id/editUserProfileFragment" />
<action
android:id="@+id/action_homeFragment_to_tripDetailsFragment"
app:destination="@id/tripDetailsFragment" />
<action
android:id="@+id/action_homeFragment_to_editTripFragment"
app:destination="@id/editTripFragment" />
</fragment>
<fragment
android:id="@+id/helpFragment"
android:name="com.gochapperone.chapperone.ui.settings.HelpFragment"
android:label="help"
tools:layout="@layout/fragment_help"/>