I have one MainActivity with two fragments, FirstFragment and SecondFragment, in my Activity i have an onOptionItemSelected
handler from which the user should be able to navigate to settings.
The issue is that the handler looks like this:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> {
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
navController.navigate(R.id.action_FirstFragment_to_settingsActivity)
return true
}
else -> super.onOptionsItemSelected(item)
}
}
So i have no problems to navigate from FirstFragment to SettingsActivity, but when i try to navigate to Settings activity from my SecondFragment my app crash as the navigate action is set only for first fragment...
So in that handler how can i check in which fragment i'm and cast the action_SecondFragment_to_settingsActivity
?