0

I am using 2 bottom navigation views and both have separate nav graphs and a different navGraph for auth-related fragments. So I have 3 activities in-app and multiple fragments The navigation component is not going back to the previous fragment on the back press, it reloads the current fragment again and again. Is there any better approach for this and how can I change its behavior.

<fragment
        android:id="@+id/fragNavHost"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/bottom_nav_graph" />
TBA
  • 1,921
  • 4
  • 13
  • 26
Nadeem Aslam
  • 123
  • 1
  • 7
  • navigation 2.4.0 or 2.3.x? 2.3.x' s sample code can do this. – Krahmal Jan 26 '22 at 05:33
  • activity?.findNavController(R.id.fragNavHost)?.popBackStack() – RanaUmer Jan 26 '22 at 05:58
  • "is not going back to previous fragment on back press, it reload the current fragment again and again" - it sounds like you have a `LiveData` you `observe` in the fragment that you are going back to that is calling `navigate()`. Is that correct? Can you include the code for that fragment you are trying to go back to? – ianhanniballake Jan 26 '22 at 06:09
  • @ianhanniballake you are right. i think live data observer is creating the problem. Using custom single live event will solve the problem. viewModel.loginResponse.observe(this, { when (it) { is NetworkResult.Success -> { findNavController().navigate(LoginFragmentDirections.actionLoginToSignupTypes()) } – Nadeem Aslam Jan 27 '22 at 06:20

0 Answers0