activity_main.xml
<androidx.fragment.app.FragmentContainerView
android:id="@ id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" >
</androidx.fragment.app.FragmentContainerView>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/bottom_controls"
android:layout_width="match_parent"
android:layout_height="72dp"
android:onClick="onSongInfoClick"
android:background="@drawable/lyt_rounded_bottom_music"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:elevation="25dp"
app:layout_constraintTop_toBottomOf="@id/nav_host_fragment">
The start destination is a ViewPagerFragment consisting of various tabs.
When I click the bottom_controls view from activity_main I am navigating to SongDetailsFragment
I don't want to show that bottom_controls in songDetailsFragment
Here's how i'm trying to do it:
MainActivity.kt
val currentFragment = supportFragmentManager.currentNavigationFragment
if(currentFragment !== SongDetailsFragment())
{
viewModel.showMiniPlayer()
}
else{
viewModel.hideMiniPlayer()
}
SongDetailsFragment.kt
class SongDetailsFragment : BaseSongDetailFragment() {
.....
}
BaseSongDetailFragment.kt
open class BaseSongDetailFragment : Fragment() {
private val songDetailViewModel by sharedViewModel<SongDetailViewModel>()
protected val mainViewModel by inject<MainViewModel>()
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
showHideBottomSheet()
}
override fun onPause() {
showHideBottomSheet()
super.onPause()
}
override fun onResume() {
showHideBottomSheet()
super.onResume()
}
private fun showHideBottomSheet() {
val currentData = songDetailViewModel.currentData.value ?: return
if (currentData.id == 0L) return
val currentFragment =
requireActivity().supportFragmentManager.currentNavigationFragment
if(currentFragment == this as SongDetailsFragment){
mainViewModel.hideMiniPlayer()
}
else{
mainViewModel.showMiniPlayer()
}
}
}
Problem:
The very first time I launch the app and navigate to songDetailsFragment, it shows the bottom_controls view.
Subsequently, it doesn't show.
I want it to not show at all.
CodePudding user response:
You can use OnDestinationChangedListener
in your activity to listen to changes in currentDestination.
navController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id == R.id.songDetailsFragment)
viewModel.hideMiniPlayer()
else
viewModel.showMiniPlayer()
}