Home android Navigation Drawer does not open the desired fragment

Navigation Drawer does not open the desired fragment




The application uses Navigation Drawer Activity. Navigation Architecture Component is used to go on the menu items of the Navigation Drawer. The menu items themselves are fragments. When you click on the menu item, the desired fragment is not covered. The entire Navigation Drawer code is taken from the Android Studio template. When you start this template, everything works, nothing happens in my application.

xml with navigation:

& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
& lt; navigation XMLNS: Android = "http://schemas.android.com/apk/res/android"
  XMLNS: App = "http://schemas.android.com/apk/res-auto"
  XMLNS: Tools = "http://schemas.android.com/tools"
  Android: id = "@ + ID / Mobile_navigation"
  App: StartDestination = "@ + id / nav_view_pager_fragment" & gt;
  & lt; fragment
    Android: id = "@ + id / nav_view_pager_fragment"
    Android: name = "com.epam.oleksandr_filonenko.homework_seven.ui.viewpager.viewpagerfragment"
    Android: label = "@ string / view_pager"
    Tools: layout = "@ Layout / Fragment_View_Pager" / & gt;
  & lt; fragment
    Android: id = "@ + id / nav_web_view_fragment"
 Android: name = "com.epam.oleksandr_filonenko.homework_seven.ui.webview.WebViewFragment"
    Android: label = "@ String / Web_View"
    Tools: Layout = "@ layout / fragment_web_view" / & gt;
  & lt; fragment
    Android: id = "@ + id / nav_recycler_view_fragment"
Android: name = "com.epam.oleksandr_filonenko.homework_seven.ui.recyclerview.RecyclerViewFragment"
    Android: label = "@ String / Recycler_View"
    Tools: Layout = "@ layout / fragment_recycler_view" / & gt;
& lt; / navigation & gt;

MainActivity code:

override fun oncreate (SavedInstanceState: Bundle?) {
    super.oncreate (SavedInstanceState)
    SetContentView (R.Layout.Activity_Main)
    Val DrawerLayout: DrawerLayout = FindViewByid (R.ID.Drawer_Layout)
    Val NavView: NavigationView = FindViewByid (R.ID.Nav_View)
    Val Navcontroller = FindnavController (R.ID.Nav_Host_Fragment)
    AppBarConfiguration = AppBarConfiguration (
      SETOF (
        R.id.nav_view_pager_fragment, r.id.nav_web_view_fragment,
      ), DrawerLayout
    SetupActionBarWithNavController (Navcontroller, AppBarconfiguration)
    NavView.SetupWithnavController (Navcontroller)
  Override Fun OnSupportNavigateUp (): Boolean {
    Val Navcontroller = FindnavController (R.ID.Nav_Host_Fragment)
    Return navcontroller.navigateUp (AppBarconfiguration) || super.onsupportnavigateUp ()

p.s. From the activat code was removed toolbar and the menu invested in it, as well as the Fab, which were in the template. Also, the Drawer itself is not correctly displayed.

Answer 1

The fact is that when you delete Toolbara , the index Item Menu is knocked down for your fragment. To solve this issue, you need to register Android Navigation Driver here Link .

Answer 2

Solution: In order to open the desired fragment, the menu item ID and navigation ID should be the same. The system itself will give the desired fragment to the menu item Navigation Drawer’a

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions