Reputation: 1
I want to create user flow like this: Login screen (w/o navigation bar) -> Home Screen (with navigation bar, includes backstacks for Album and Profile)
Application starts with an authentication screen. When the user clicks “Login” - I remove it from backstack using “to_albums_action” and navigate to the albums screen.
Everything works great except strange stack behavior. When the user goes from Albums to Profile, when clicks Albums again - android opens it as a new instance.
So, when the user goes: Albums -> Profile -> Albums, then starts pressing the back button he starts hopping back: -> Profile -> Albums -> Homescreen.
Expected behavior is: go directly to Homescreen, as Albums is the first element in the menu.
Digging around, I found that this behavior could be fixed if I won’t remove auth_nav from backstack. Maybe, because NavController calculates all includes for the menu. I also noticed this in logs:
I/NavController: Ignoring popBackStack to destination com.project:id/signin_dest as it was not found on the current back stack
There some of my files:
main_nav.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_nav">
<include app:graph="@navigation/auth_nav" />
<include app:graph="@navigation/profile_nav" />
<include app:graph="@navigation/album_nav" />
<action
android:id="@+id/to_login_action"
app:destination="@id/auth_nav"
app:popUpTo="@id/auth_nav"
app:popUpToInclusive="false" />
<action
android:id="@+id/to_albums_action"
app:destination="@id/album_nav"
app:popUpTo="@id/auth_nav" />
</navigation>
auth_nav.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"
android:id="@+id/auth_nav"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@+id/signin_dest">
<fragment
android:id="@+id/signin_dest"
android:name="com.artemromanov.navigationplayground.ui.signin.SignInFragment"
android:label="SignInFragment"
tools:layout="@layout/fragment_sign_in">
</fragment>
</navigation>
This is how I setup navigation in my MainActivity.kt inside onCreate()
private fun setNavigation(startDestination: Int) {
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
val navController = navHostFragment.navController
findViewById<BottomNavigationView>(R.id.bottom_navigator).setupWithNavController(navController)
navController.addOnDestinationChangedListener { _, destination, _ ->
Log.d("Activ", "${destination.id}, ${R.id.auth_nav}")
when (destination.id) {
R.id.signin_dest -> binding.bottomNavigator.visibility = View.GONE
else -> binding.bottomNavigator.visibility = View.VISIBLE
}
}
val navGraph = navController.navInflater.inflate(R.navigation.main_nav)
navGraph.setStartDestination(startDestination)
navController.graph = navGraph
}
nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/album_nav"
android:title="Albums" />
<item
android:id="@+id/profile_nav"
android:title="Profile" />
</menu>
Am I doing something wrong?
Upvotes: 0
Views: 309
Reputation: 274
You just want a single instance of your fragment so when you are clicking albums inside the profile fragment you can just use the previous instance of albums.
Here, These answers will help you :
Upvotes: 0