I have an activity_main.xml
with an BottomNavigationView
and a NavHostFragment
.
There are 2 tabs, HomeFragment
with a video and FavoritesFragment
. Within FavoritesFragment
there is a list of favorite video's (thumbnails) and when I click on an item it loads the HomeFragment
view with that specific video.
within mobile_navigation.xml
I have defined the HomeFragment
twice. 1 is the Home Tab and 1 is the "Favorite" when clicked on the thumbnail in FavoritesFragment
.
When I click the back button I expect to be back in the FavoriteFragment
. But the App shuts down.
When the thumbnail is clicked, this code is executed:
val action = FavoritesFragmentDirections
.actionNavigationFavoritesToNavigationFavorite(imageUri)
NavHostFragment.findNavController(this@FavoritesFragment)
.navigate(action)
I also tried this:
protected fun showFragment(fragment: Fragment, uniqueName: String) {
activity?.supportFragmentManager
?.beginTransaction()
?.replace(R.id.container, fragment)
?.addToBackStack(uniqueName)
?.commitAllowingStateLoss()
}
But same result.
With this:
override fun onBackPressed() {
super.onBackPressed()
if (!navController.popBackStack()) {
// Call finish() on your Activity
finish()
}
}
The back button goes back to the HomeFragment
, instead of FavoritesFragment
.
<?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/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="eu.theappfactory.dailyrecipes.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home">
<argument
android:name="imageuri"
app:argType="string" />
</fragment>
<fragment
android:id="@+id/navigation_favorites"
android:name="eu.theappfactory.dailyrecipes.ui.favorites.FavoritesFragment"
android:label="@string/title_account"
tools:layout="@layout/fragment_favorites" >
<action
android:id="@+id/action_navigation_favorites_to_navigation_favorite"
app:destination="@id/navigation_favorite" />
</fragment>
<fragment
android:id="@+id/navigation_favorite"
android:name="eu.theappfactory.dailyrecipes.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home">
<argument
android:name="imageuri"
app:argType="string" />
</fragment>
</navigation>
from Previous fragment not loaded when pressing Back button using BottomNavigationView
No comments:
Post a Comment