I have an activity and 2 fragments bound to a navigation system. So, I have 2 destinations.
If I switch destinations via the BottomNavigationView-menu - it works fine!
But if I call navController.navigate(destination2)
from destination1 I can't return to destination1 via menu until click back button from destination2.
MainActivity:
...
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
super.onCreate(savedInstanceState)
// -- setting a navigation
val navView: BottomNavigationView = findViewById(R.id.bottom_navigation)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
val navController = navHostFragment.navController
navView.setupWithNavController(navController)
}
...
NavGraph.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/navigation"
app:startDestination="@+id/navigation_frag1">
<fragment
android:id="@+id/navigation_frag1"
android:label="DESTINATION-1"
android:name="com.my.app.fragments.Fragment1"/>
<fragment
android:id="@+id/navigation_frag2"
android:label="DESTINATION-2"
android:name="com.my.app.fragments.Fragment2"/>
</navigation>
Fragment1.kt:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// -- fragment view
val view = inflater.inflate(R.layout.fragment_1, null)
// -- navController
val nc = requireActivity().findNavController(R.id.nav_host_fragment_activity_main )
// -- call destination2 via txt-button
view.findViewById<TextView>(R.id.callBtn).setOnClickListener {
nc.navigate( R.id.navigation_frag2 )
}
return view
}
I hoped to exist a transparency between switching destinations via menu and programmatically way. How to achieve this?