SetupWithNavController does't work for toolbar












0















I am trying to bind toolbar to navigation controller, for that I am using the following code:



NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


and in the menu file, I provided the id of the fragment to which app should navigate like this:



<item
android:id="@+id/menuFragment"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />


and I have a simple navigation graph file like this:



<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/nav_graph"
app:startDestination="@id/homeFragment">

<fragment
android:id="@+id/homeFragment"
android:name="com.vapoyan.myapplication.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@+id/action_homeFragment_to_menuFragment"
app:destination="@id/menuFragment" />
</fragment>

<fragment
android:id="@+id/menuFragment"
android:name="com.vapoyan.myapplication.MenuFragment"
android:label="fragment_menu"
tools:layout="@layout/fragment_menu" />
</navigation>


Does anyone have experience or can suggest how I can fix the issue and is toolbar supported by the navigation component?



Any example code or reference?










share|improve this question





























    0















    I am trying to bind toolbar to navigation controller, for that I am using the following code:



    NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


    and in the menu file, I provided the id of the fragment to which app should navigate like this:



    <item
    android:id="@+id/menuFragment"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />


    and I have a simple navigation graph file like this:



    <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/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
    android:id="@+id/homeFragment"
    android:name="com.vapoyan.myapplication.HomeFragment"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" >
    <action
    android:id="@+id/action_homeFragment_to_menuFragment"
    app:destination="@id/menuFragment" />
    </fragment>

    <fragment
    android:id="@+id/menuFragment"
    android:name="com.vapoyan.myapplication.MenuFragment"
    android:label="fragment_menu"
    tools:layout="@layout/fragment_menu" />
    </navigation>


    Does anyone have experience or can suggest how I can fix the issue and is toolbar supported by the navigation component?



    Any example code or reference?










    share|improve this question



























      0












      0








      0








      I am trying to bind toolbar to navigation controller, for that I am using the following code:



      NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


      and in the menu file, I provided the id of the fragment to which app should navigate like this:



      <item
      android:id="@+id/menuFragment"
      android:orderInCategory="100"
      android:title="@string/action_settings"
      app:showAsAction="never" />


      and I have a simple navigation graph file like this:



      <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/nav_graph"
      app:startDestination="@id/homeFragment">

      <fragment
      android:id="@+id/homeFragment"
      android:name="com.vapoyan.myapplication.HomeFragment"
      android:label="fragment_home"
      tools:layout="@layout/fragment_home" >
      <action
      android:id="@+id/action_homeFragment_to_menuFragment"
      app:destination="@id/menuFragment" />
      </fragment>

      <fragment
      android:id="@+id/menuFragment"
      android:name="com.vapoyan.myapplication.MenuFragment"
      android:label="fragment_menu"
      tools:layout="@layout/fragment_menu" />
      </navigation>


      Does anyone have experience or can suggest how I can fix the issue and is toolbar supported by the navigation component?



      Any example code or reference?










      share|improve this question
















      I am trying to bind toolbar to navigation controller, for that I am using the following code:



      NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


      and in the menu file, I provided the id of the fragment to which app should navigate like this:



      <item
      android:id="@+id/menuFragment"
      android:orderInCategory="100"
      android:title="@string/action_settings"
      app:showAsAction="never" />


      and I have a simple navigation graph file like this:



      <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/nav_graph"
      app:startDestination="@id/homeFragment">

      <fragment
      android:id="@+id/homeFragment"
      android:name="com.vapoyan.myapplication.HomeFragment"
      android:label="fragment_home"
      tools:layout="@layout/fragment_home" >
      <action
      android:id="@+id/action_homeFragment_to_menuFragment"
      app:destination="@id/menuFragment" />
      </fragment>

      <fragment
      android:id="@+id/menuFragment"
      android:name="com.vapoyan.myapplication.MenuFragment"
      android:label="fragment_menu"
      tools:layout="@layout/fragment_menu" />
      </navigation>


      Does anyone have experience or can suggest how I can fix the issue and is toolbar supported by the navigation component?



      Any example code or reference?







      android kotlin android-architecture-components androidx






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 9:40







      Victor Apoyan

















      asked Nov 23 '18 at 9:33









      Victor ApoyanVictor Apoyan

      907819




      907819
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I am using a Navigation drawer in my app, but i think this will be enough for ur case:



          At this moment i am using this android libs:
          In my build.gradle



           implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha07'
          implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha07'


          In my Activity



          class MainActivity : BaseActivity() {

          @SuppressLint("RestrictedApi")
          override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.main_activity)

          val navController = Navigation.findNavController(this, /*ur nav controller ID*/)

          // Set up ActionBar
          setSupportActionBar(/*ur toolbar*/)

          NavigationUI.setupActionBarWithNavController(this, navController)
          }
          }


          U can check the docs here u can find more info: https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#create_a_toolbar






          share|improve this answer





















          • 1





            I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

            – Victor Apoyan
            Nov 23 '18 at 9:57





















          0














          Finally, I found a solution thanks to Google CodeLab



          What I was missing was:



          override fun onOptionsItemSelected(item: MenuItem): Boolean {
          return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host))
          || super.onOptionsItemSelected(item)
          }


          Plus if you want to have back button support you need in onCreate method to add:



          NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


          Basically, In my understanding just bt providing id of the correct fragment for the menu item and calling setupWithNavController should work, but that assumption was not correct, or maybe in the current version (1.0.0-alpha07) Google guys changed something. So now it is working fine.



          If you see that there is a way to do it shorter :) or better :) let me know.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443965%2fsetupwithnavcontroller-doest-work-for-toolbar%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I am using a Navigation drawer in my app, but i think this will be enough for ur case:



            At this moment i am using this android libs:
            In my build.gradle



             implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha07'
            implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha07'


            In my Activity



            class MainActivity : BaseActivity() {

            @SuppressLint("RestrictedApi")
            override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main_activity)

            val navController = Navigation.findNavController(this, /*ur nav controller ID*/)

            // Set up ActionBar
            setSupportActionBar(/*ur toolbar*/)

            NavigationUI.setupActionBarWithNavController(this, navController)
            }
            }


            U can check the docs here u can find more info: https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#create_a_toolbar






            share|improve this answer





















            • 1





              I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

              – Victor Apoyan
              Nov 23 '18 at 9:57


















            0














            I am using a Navigation drawer in my app, but i think this will be enough for ur case:



            At this moment i am using this android libs:
            In my build.gradle



             implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha07'
            implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha07'


            In my Activity



            class MainActivity : BaseActivity() {

            @SuppressLint("RestrictedApi")
            override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main_activity)

            val navController = Navigation.findNavController(this, /*ur nav controller ID*/)

            // Set up ActionBar
            setSupportActionBar(/*ur toolbar*/)

            NavigationUI.setupActionBarWithNavController(this, navController)
            }
            }


            U can check the docs here u can find more info: https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#create_a_toolbar






            share|improve this answer





















            • 1





              I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

              – Victor Apoyan
              Nov 23 '18 at 9:57
















            0












            0








            0







            I am using a Navigation drawer in my app, but i think this will be enough for ur case:



            At this moment i am using this android libs:
            In my build.gradle



             implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha07'
            implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha07'


            In my Activity



            class MainActivity : BaseActivity() {

            @SuppressLint("RestrictedApi")
            override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main_activity)

            val navController = Navigation.findNavController(this, /*ur nav controller ID*/)

            // Set up ActionBar
            setSupportActionBar(/*ur toolbar*/)

            NavigationUI.setupActionBarWithNavController(this, navController)
            }
            }


            U can check the docs here u can find more info: https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#create_a_toolbar






            share|improve this answer















            I am using a Navigation drawer in my app, but i think this will be enough for ur case:



            At this moment i am using this android libs:
            In my build.gradle



             implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha07'
            implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha07'


            In my Activity



            class MainActivity : BaseActivity() {

            @SuppressLint("RestrictedApi")
            override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main_activity)

            val navController = Navigation.findNavController(this, /*ur nav controller ID*/)

            // Set up ActionBar
            setSupportActionBar(/*ur toolbar*/)

            NavigationUI.setupActionBarWithNavController(this, navController)
            }
            }


            U can check the docs here u can find more info: https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#create_a_toolbar







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 23 '18 at 9:52

























            answered Nov 23 '18 at 9:44









            CatlucCatluc

            920717




            920717








            • 1





              I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

              – Victor Apoyan
              Nov 23 '18 at 9:57
















            • 1





              I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

              – Victor Apoyan
              Nov 23 '18 at 9:57










            1




            1





            I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

            – Victor Apoyan
            Nov 23 '18 at 9:57







            I found a solution I was missing call to override fun onOptionsItemSelected(item: MenuItem): Boolean { return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host)) || super.onOptionsItemSelected(item) }

            – Victor Apoyan
            Nov 23 '18 at 9:57















            0














            Finally, I found a solution thanks to Google CodeLab



            What I was missing was:



            override fun onOptionsItemSelected(item: MenuItem): Boolean {
            return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host))
            || super.onOptionsItemSelected(item)
            }


            Plus if you want to have back button support you need in onCreate method to add:



            NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


            Basically, In my understanding just bt providing id of the correct fragment for the menu item and calling setupWithNavController should work, but that assumption was not correct, or maybe in the current version (1.0.0-alpha07) Google guys changed something. So now it is working fine.



            If you see that there is a way to do it shorter :) or better :) let me know.






            share|improve this answer




























              0














              Finally, I found a solution thanks to Google CodeLab



              What I was missing was:



              override fun onOptionsItemSelected(item: MenuItem): Boolean {
              return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host))
              || super.onOptionsItemSelected(item)
              }


              Plus if you want to have back button support you need in onCreate method to add:



              NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


              Basically, In my understanding just bt providing id of the correct fragment for the menu item and calling setupWithNavController should work, but that assumption was not correct, or maybe in the current version (1.0.0-alpha07) Google guys changed something. So now it is working fine.



              If you see that there is a way to do it shorter :) or better :) let me know.






              share|improve this answer


























                0












                0








                0







                Finally, I found a solution thanks to Google CodeLab



                What I was missing was:



                override fun onOptionsItemSelected(item: MenuItem): Boolean {
                return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host))
                || super.onOptionsItemSelected(item)
                }


                Plus if you want to have back button support you need in onCreate method to add:



                NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


                Basically, In my understanding just bt providing id of the correct fragment for the menu item and calling setupWithNavController should work, but that assumption was not correct, or maybe in the current version (1.0.0-alpha07) Google guys changed something. So now it is working fine.



                If you see that there is a way to do it shorter :) or better :) let me know.






                share|improve this answer













                Finally, I found a solution thanks to Google CodeLab



                What I was missing was:



                override fun onOptionsItemSelected(item: MenuItem): Boolean {
                return NavigationUI.onNavDestinationSelected(item, NavHostFragment.findNavController(nav_host))
                || super.onOptionsItemSelected(item)
                }


                Plus if you want to have back button support you need in onCreate method to add:



                NavigationUI.setupWithNavController(toolbar, NavHostFragment.findNavController(nav_host))


                Basically, In my understanding just bt providing id of the correct fragment for the menu item and calling setupWithNavController should work, but that assumption was not correct, or maybe in the current version (1.0.0-alpha07) Google guys changed something. So now it is working fine.



                If you see that there is a way to do it shorter :) or better :) let me know.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 9:57









                Victor ApoyanVictor Apoyan

                907819




                907819






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443965%2fsetupwithnavcontroller-doest-work-for-toolbar%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    To store a contact into the json file from server.js file using a class in NodeJS

                    Redirect URL with Chrome Remote Debugging Android Devices

                    Dieringhausen