Skip to content Skip to sidebar Skip to footer

Android Navigation Component Up Button, Arrow Displays But Popto Not Working

I'm using the jetpack navigation component with a very simple set up, one activity (MainActivity) and 4 fragments MainFragment, CardPreviewFragment, createCardFragment, and AddP

Solution 1:

When using setupActionBarWithNavController(), you need to override onSupportNavigateUp() to trigger the Up icon in the Action Bar as per the documentation:

@OverridepublicbooleanonSupportNavigateUp() {
    returnNavigation.findNavController(this, R.id.main_fragment).navigateUp()
            || super.onSupportNavigateUp();
}

Solution 2:

In addition if you want enable up button for fragments you can subclass the toolbar and

overridefunonAttachedToWindow() {
    super.onAttachedToWindow()
    val navController = (context as AppCompatActivity).supportFragmentManager.findFragmentById(R.id.nav_host)!!.findNavController()
    setupWithNavController(navController)
}

and write something like above inside of it , assuming that you're using AppcompatActivity which has your NavHostFragment so it has a nav controller , so you can set it up with navcontroller here instead of doing this in every fragment as documentations says.

subclassing toolbar has more advantages like you can set a back icon for all fragments in just a single place also you can change font and position of toolbar title which is not supported by the toolbar out of the box.

it do it some like

overridefunonLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
    super.onLayout(changed, l, t, r, b)
    children.forEach {
        if(it is TextView && !titleSet){
            titleSet  = true
            it.x -= 50
            it.typeface = ResourcesCompat.getFont(context, R.font.poppins_bold)
            return@forEach
        }
    }
}

Above solution is not tested in many apps and may not be sufficient for your use case but it worked for me.

Post a Comment for "Android Navigation Component Up Button, Arrow Displays But Popto Not Working"