How to update NavigationDrawer selected item when navigating from a Fragment?

1 day ago 2
ARTICLE AD BOX

I would like to move navigation logic from MainActivity to MainFragment. However, when navigation happens, I also need to update the selected item in the NavigationDrawer, which is defined in MainActivity.

Previously, navigation was handled inside MainActivity, where I could both navigate and update the drawer selection at the same time. Now I would like to trigger navigation from MainFragment, but still update the drawer item in MainActivity.

What is the recommended way to implement this?

For example, I would like something like this in the fragment:

fun Fragment.navigateToService() { findNavController().navigate( MainFragmentDirections.actionMainFragmentToServiceListFragment() ) }

But at the same time I need to update the selected item in the NavigationDrawer that is owned by MainActivity.

What is the cleanest way to communicate this from a fragment to its hosting activity?

Read Entire Article