android navigation component up button, arrow displays but popTo not working android navigation component up button, arrow displays but popTo not working android android

android navigation component up button, arrow displays but popTo not working


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

@Overridepublic boolean onSupportNavigateUp() {    return Navigation.findNavController(this, R.id.main_fragment).navigateUp()            || super.onSupportNavigateUp();}


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

override fun onAttachedToWindow() {    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

override fun onLayout(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.