architecture-components-samples
architecture-components-samples copied to clipboard
Bottom navigation with splash
I'm implementing this bottom navigation pattern but with a splash fragment.
My issue is when I navigate throw different fragments with bottom menu and I press to go back, I don't go back to the home fragment, instead of this, I return to the previous fragment.
For example, I have fragments A-B-C:
Now I'm on fragment A and I press to go to B. Then I press to go to C (from B). Then I press to go back. The result is I'm getting back to B, not to fragment A (what I really want!). (If I set in the navigation graph app:startDestination -> fragment A - instead of login fragment - everything goes well).
Here is my graph:
<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" app:startDestination="@id/splashFragment">
<fragment
android:id="@+id/splashFragment"
android:name="application.SplashFragment"
tools:layout="@layout/fragment_splash">
<action
android:id="@+id/action_splashFragment_to_fragmentA"
app:destination="@id/fragmentA"
app:popUpTo="@id/main_navigation"
app:popUpToInclusive="true" />
</fragment>
<fragment
android:id="@+id/fragmentA"
android:name="application.fragmentA"
android:label="@string/fragmentA"
tools:layout="@layout/fragmentA" />
<fragment
android:id="@+id/fragmentB"
android:name="application.fragmentB"
android:label="fragmentB"
tools:layout="@layout/fragmentB" />
<fragment
android:id="@+id/fragmentC"
android:name="application.fragmentC"
android:label="@string/fragmentC"
tools:layout="@layout/fragmentC" />
And here my MainActivity:
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Toolbar & Navigation
setSupportActionBar(toolbar)
navController = findNavController(R.id.nav_host)
// AppBarConfiguration with the correct top-level destinations
appBarConfiguration = AppBarConfiguration(
topLevelDestinationIds = setOf(
R.id.fragmentA,
R.id.fragmentB,
R.id.fragmentC
)
)
// This allows NavigationUI to decide what label to show in the action bar
// By using appBarConfig, it will also determine whether to
// show the up arrow or drawer menu icon
setupActionBarWithNavController(navController, appBarConfiguration)
// Set destinations to left and bottom navigation
bottom_navigation.setupWithNavController(navController)
// Set visibility for NavigationView & Toolbar
visibilityNavElements()
}
// Allows NavigationUI to support proper up navigation or the drawer layout
// drawer menu, depending on the situation
override fun onSupportNavigateUp() = navController.navigateUp(appBarConfiguration)
private fun visibilityNavElements() {
findNavController(R.id.nav_host).addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
R.id.splashFragment -> {
toolbar.visibility = View.GONE
bottom_navigation.visibility = View.GONE
}
else -> {
toolbar.visibility = View.VISIBLE
bottom_navigation.visibility = View.VISIBLE
}
}
}
}
}
Thanks!
I've got an example here showing the behaviour you're looking for using. I use Fragments only instead of the Activity, and the Bottom Navigation View is hosted in it's own Fragment so you don't need to bother hiding and showing the BottomNavigationView on your Splash screen or anywhere else because you can just navigate to different Fragments instead
same here, Does anyone have a solution for this case?
I am also facing the same issue .. is there any solution? Without Splash everything works well.