Skip to content Skip to sidebar Skip to footer

Navigation Drawer Unlock Mode Ignored

So I have this app with one activity (MainActivity) and I'm using the navigation architecture component. So it has DrawerLayout which is wrapping the fragment view. These are two m

Solution 1:

If you look at the ordering of the callbacks, you'll see that the onViewCreated() of the new Fragment is called before the onDestroyView() of the old Fragment. This makes particular sense when it comes to animations since to smoothly animate between two Fragments, both the new View and the old View must coexist and only after the animation ends does the old Fragment get destroyed. Fragments, in order to be consistent, use the same ordering whether you have an animation or not.

Ideally, your Fragments shouldn't be reaching up to the Activity at all to affect the Activity's behavior. As per the Listen for Navigation Events documentation, you should instead use an OnDestinationChangedListener to update your Activity's UI. The listener only receives information about the destination and its arguments however, so to use this approach you'd want to add an argument to your navigation graph:

<!-- this destination leaves the drawer in its default unlocked state --><fragmentandroid:id="@+id/fragment_with_drawer"android:name=".MainFragment" /><!-- the drawer is locked when you're at this destination --><fragmentandroid:id="@+id/fragment_with_locked_drawer"android:name=".SettingsFragment"><argumentandroid:name="lock_drawer"android:defaultValue="true"/></fragment>

Then your OnDestinationChangedListener could look like

navController.addOnDestinationChangedListener { _, _, arguments ->
  if(arguments?.getBoolean("lock_drawer", false) == true) {
    lockNavDrawer()
  } else {
    unlockNavDrawer()
  }
}

Post a Comment for "Navigation Drawer Unlock Mode Ignored"