I made so much research about that but can't find a solution, I can't figure out what's wrong. I'm using the navigation component library, and single activity. When a user log out it take the user to the login the bottom nav is removed it left a blank space, and it's like the whole layout has been moved up a bit(img 1). Then when you log in, the design seem moved up like before but you have bottom nav view displayed and appbar displayed(img 2). That is weird I just can't figure out where I got this wrong. The app flow goes like this: when user open app, it take him to HomeFragment, if he's already logged in he access the homeFragment, otherwise the homefragment check if he's not login it take him to login fragment. But when the user close app and relaunch it display well, or if the user is already loggin and open the app, it display well. Error happen: when user log out and is taken to login screen, then upon login it's still there until app restart. Sorry for the ugliness of that post :-\
Here's the Activity code that remove bottom bar view and also app bar when you're in login screen:
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()
}
Here's the xml for MainActivity:
<?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>
Here's an except of the code for Login fragment, and his Layout height and width is set to 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)
Here's an except for 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)
}
Here's except from navigation graph 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"/>