0

I'm implementing this bottom navigation pattern but with a splash fragment.

My issue is when I navigate throw different fragments with bottom menu and I press to go back, I don't go back to the home fragment, instead of this, I return to the previous fragment.

For example, I have fragments A-B-C:

  • Now I'm on fragment A and I press to go to B.
  • Then I press to go to C (from B).
  • Then I press to go back.
  • The result is I'm getting back to B, not to fragment A (what I really want!).

(If I set in the navigation graph app:startDestination -> fragment A - instead of login fragment - everything goes well).

Here is my graph:

<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_navigation"
app:startDestination="@id/splashFragment">

<fragment
    android:id="@+id/splashFragment"
    android:name="application.SplashFragment"
    tools:layout="@layout/fragment_splash">
    <action
        android:id="@+id/action_splashFragment_to_fragmentA"
        app:destination="@id/fragmentA"
        app:popUpTo="@id/main_navigation"
        app:popUpToInclusive="true" />
</fragment>

<fragment
    android:id="@+id/fragmentA"
    android:name="application.fragmentA"
    android:label="@string/fragmentA"
    tools:layout="@layout/fragmentA" />

<fragment
    android:id="@+id/fragmentB"
    android:name="application.fragmentB"
    android:label="fragmentB"
    tools:layout="@layout/fragmentB" />

<fragment
    android:id="@+id/fragmentC"
    android:name="application.fragmentC"
    android:label="@string/fragmentC"
    tools:layout="@layout/fragmentC" />
    

And here my MainActivity:

class MainActivity : AppCompatActivity() {

private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // Toolbar & Navigation
    setSupportActionBar(toolbar)
    navController = findNavController(R.id.nav_host)

    // AppBarConfiguration with the correct top-level destinations
    appBarConfiguration = AppBarConfiguration(
            topLevelDestinationIds = setOf(
                    R.id.fragmentA,
                    R.id.fragmentB,
                    R.id.fragmentC
            )
    )

    // This allows NavigationUI to decide what label to show in the action bar
    // By using appBarConfig, it will also determine whether to
    // show the up arrow or drawer menu icon
    setupActionBarWithNavController(navController, appBarConfiguration)

    // Set destinations to left and bottom navigation
    bottom_navigation.setupWithNavController(navController)

    // Set visibility for NavigationView & Toolbar
    visibilityNavElements()
}

// Allows NavigationUI to support proper up navigation or the drawer layout
// drawer menu, depending on the situation
override fun onSupportNavigateUp() = navController.navigateUp(appBarConfiguration)

private fun visibilityNavElements() {
    findNavController(R.id.nav_host).addOnDestinationChangedListener { _, destination, _ ->
        when (destination.id) {
            R.id.splashFragment -> {
                toolbar.visibility = View.GONE
                bottom_navigation.visibility = View.GONE
            }
            else -> {
                toolbar.visibility = View.VISIBLE
                bottom_navigation.visibility = View.VISIBLE
            }
        }
    }
}

}

Thanks!

gmalija
  • 25
  • 3

1 Answers1

0

You can handle OnbackPress in any fragment like below and is the recommended way . You can use it in Fragment C and when back is pressed you can navigate to Fragment A

in Java

 OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(true) {
    @Override
    public void handleOnBackPressed() {


    }
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);

in Kotlin

 val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
        // Handle the back button event
    }
Wahdat Jan
  • 3,988
  • 3
  • 21
  • 46
  • Thanks for the answer! But I don't really understand you. What you are saying is going back to A catching the on back press result in fragments B and C? What I really don't understand is this behaviour of bottom navigation when I have a previous fragment, in this case SplashFragment. – gmalija Nov 19 '20 at 16:20