I'm trying (without success) to change the back arrow icon on the toolbar when using NavHostFragment. I'm using NavController with toolbar (as you can see here)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
toolbar.setTitleTextColor(resources.getColor(R.color.toolbarTextColor))
navigation.setupWithNavController(Navigation.findNavController(this, R.id.nav_host_fragment))
}
The navigation is working properly, but i can't seem to change the back icon.
I've already tried using a custom theme. None of the atributes defined have worked so far.
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar">
<item name="android:navigationIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="android:homeAsUpIndicator">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="collapseIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
<item name="android:closeIcon">@drawable/ic_keyboard_arrow_left_black_24dp</item>
</style>
Tried using code:
toolbar.navigationIcon = resources.getDrawable(R.drawable.ic_keyboard_arrow_left_black_24dp)
toolbar.setNavigationIcon(R.drawable.ic_keyboard_arrow_left_black_24dp)
toolbar.navigationIconResource = R.drawable.ic_keyboard_arrow_left_black_24dp
When using code, once the layout is created the back button appears but when i enter another fragment, the icon changes back to the default back icon.
How can i change it?