how do you pass arguments to fragment called from NavigationDrawer using NavigationUI?












1















I've converted my app to use NavigationUI. But setupWithNavController() replaces my setNavigationItemSelectedListener().



In my listener I was passing a (type safe) argument to the fragment.










share|improve this question





























    1















    I've converted my app to use NavigationUI. But setupWithNavController() replaces my setNavigationItemSelectedListener().



    In my listener I was passing a (type safe) argument to the fragment.










    share|improve this question



























      1












      1








      1








      I've converted my app to use NavigationUI. But setupWithNavController() replaces my setNavigationItemSelectedListener().



      In my listener I was passing a (type safe) argument to the fragment.










      share|improve this question
















      I've converted my app to use NavigationUI. But setupWithNavController() replaces my setNavigationItemSelectedListener().



      In my listener I was passing a (type safe) argument to the fragment.







      android-architecture-navigation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 16:59









      common sense

      2,57351625




      2,57351625










      asked Nov 15 '18 at 16:54









      Simon KenyonSimon Kenyon

      62




      62
























          2 Answers
          2






          active

          oldest

          votes


















          0














          If I understand your question correct what you are looking for is:



          Navigation parameters documentation



          Briefly:



          In your navigation graph, on the fragment you want the value sent to, you will add an argument.



          <fragment android:id="@+id/myFragment" >
          <argument
          android:name="amount"
          app:argType="integer"
          android:defaultValue="0" />
          </fragment>


          When you then reference that fragment as an action, the library will generate a Directories class, which will look something like this:



           val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
          v.findNavController().navigate(action)


          The names are generated based upon what you named the Fragment and actions.
          Also note that it is possible to send whole objects if they are Parcelable as well.



          If the Fragment in question is the startDestination of your graph I am not sure if you can do it right away. A work around might be to load the data in the Fragment? Or maybe into a shared ViewModel from the Activity?



          Good luck!






          share|improve this answer































            0














            If anyone is still looking for an answer to this question. I've found a usefull workaround with the NavigationUI Global actions (working with alpha-09)



            You can provide a top level action to your navigation graph:



            <?xml version="1.0" encoding="utf-8"?>
            <navigation xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/main_nav"
            app:startDestination="@id/mainFragment">
            ...
            <action android:id="@+id/action_global_mainFragment"
            app:destination="@id/mainFragment"/>
            </navigation>


            And use it with your navigation controller to navigate with the correct bundle when a user click on a menu item. Be carefull to remove .setupWithNavController(navController) call from your NavigationView, or it will override your behavior.






            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%2f53324347%2fhow-do-you-pass-arguments-to-fragment-called-from-navigationdrawer-using-navigat%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














              If I understand your question correct what you are looking for is:



              Navigation parameters documentation



              Briefly:



              In your navigation graph, on the fragment you want the value sent to, you will add an argument.



              <fragment android:id="@+id/myFragment" >
              <argument
              android:name="amount"
              app:argType="integer"
              android:defaultValue="0" />
              </fragment>


              When you then reference that fragment as an action, the library will generate a Directories class, which will look something like this:



               val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
              v.findNavController().navigate(action)


              The names are generated based upon what you named the Fragment and actions.
              Also note that it is possible to send whole objects if they are Parcelable as well.



              If the Fragment in question is the startDestination of your graph I am not sure if you can do it right away. A work around might be to load the data in the Fragment? Or maybe into a shared ViewModel from the Activity?



              Good luck!






              share|improve this answer




























                0














                If I understand your question correct what you are looking for is:



                Navigation parameters documentation



                Briefly:



                In your navigation graph, on the fragment you want the value sent to, you will add an argument.



                <fragment android:id="@+id/myFragment" >
                <argument
                android:name="amount"
                app:argType="integer"
                android:defaultValue="0" />
                </fragment>


                When you then reference that fragment as an action, the library will generate a Directories class, which will look something like this:



                 val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
                v.findNavController().navigate(action)


                The names are generated based upon what you named the Fragment and actions.
                Also note that it is possible to send whole objects if they are Parcelable as well.



                If the Fragment in question is the startDestination of your graph I am not sure if you can do it right away. A work around might be to load the data in the Fragment? Or maybe into a shared ViewModel from the Activity?



                Good luck!






                share|improve this answer


























                  0












                  0








                  0







                  If I understand your question correct what you are looking for is:



                  Navigation parameters documentation



                  Briefly:



                  In your navigation graph, on the fragment you want the value sent to, you will add an argument.



                  <fragment android:id="@+id/myFragment" >
                  <argument
                  android:name="amount"
                  app:argType="integer"
                  android:defaultValue="0" />
                  </fragment>


                  When you then reference that fragment as an action, the library will generate a Directories class, which will look something like this:



                   val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
                  v.findNavController().navigate(action)


                  The names are generated based upon what you named the Fragment and actions.
                  Also note that it is possible to send whole objects if they are Parcelable as well.



                  If the Fragment in question is the startDestination of your graph I am not sure if you can do it right away. A work around might be to load the data in the Fragment? Or maybe into a shared ViewModel from the Activity?



                  Good luck!






                  share|improve this answer













                  If I understand your question correct what you are looking for is:



                  Navigation parameters documentation



                  Briefly:



                  In your navigation graph, on the fragment you want the value sent to, you will add an argument.



                  <fragment android:id="@+id/myFragment" >
                  <argument
                  android:name="amount"
                  app:argType="integer"
                  android:defaultValue="0" />
                  </fragment>


                  When you then reference that fragment as an action, the library will generate a Directories class, which will look something like this:



                   val action = SpecifyAmountFragmentDirections.confirmationAction(amount)
                  v.findNavController().navigate(action)


                  The names are generated based upon what you named the Fragment and actions.
                  Also note that it is possible to send whole objects if they are Parcelable as well.



                  If the Fragment in question is the startDestination of your graph I am not sure if you can do it right away. A work around might be to load the data in the Fragment? Or maybe into a shared ViewModel from the Activity?



                  Good luck!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 1 '18 at 22:48









                  Henrik GyllensvärdHenrik Gyllensvärd

                  46638




                  46638

























                      0














                      If anyone is still looking for an answer to this question. I've found a usefull workaround with the NavigationUI Global actions (working with alpha-09)



                      You can provide a top level action to your navigation graph:



                      <?xml version="1.0" encoding="utf-8"?>
                      <navigation xmlns:app="http://schemas.android.com/apk/res-auto"
                      xmlns:tools="http://schemas.android.com/tools"
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/main_nav"
                      app:startDestination="@id/mainFragment">
                      ...
                      <action android:id="@+id/action_global_mainFragment"
                      app:destination="@id/mainFragment"/>
                      </navigation>


                      And use it with your navigation controller to navigate with the correct bundle when a user click on a menu item. Be carefull to remove .setupWithNavController(navController) call from your NavigationView, or it will override your behavior.






                      share|improve this answer




























                        0














                        If anyone is still looking for an answer to this question. I've found a usefull workaround with the NavigationUI Global actions (working with alpha-09)



                        You can provide a top level action to your navigation graph:



                        <?xml version="1.0" encoding="utf-8"?>
                        <navigation xmlns:app="http://schemas.android.com/apk/res-auto"
                        xmlns:tools="http://schemas.android.com/tools"
                        xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/main_nav"
                        app:startDestination="@id/mainFragment">
                        ...
                        <action android:id="@+id/action_global_mainFragment"
                        app:destination="@id/mainFragment"/>
                        </navigation>


                        And use it with your navigation controller to navigate with the correct bundle when a user click on a menu item. Be carefull to remove .setupWithNavController(navController) call from your NavigationView, or it will override your behavior.






                        share|improve this answer


























                          0












                          0








                          0







                          If anyone is still looking for an answer to this question. I've found a usefull workaround with the NavigationUI Global actions (working with alpha-09)



                          You can provide a top level action to your navigation graph:



                          <?xml version="1.0" encoding="utf-8"?>
                          <navigation xmlns:app="http://schemas.android.com/apk/res-auto"
                          xmlns:tools="http://schemas.android.com/tools"
                          xmlns:android="http://schemas.android.com/apk/res/android"
                          android:id="@+id/main_nav"
                          app:startDestination="@id/mainFragment">
                          ...
                          <action android:id="@+id/action_global_mainFragment"
                          app:destination="@id/mainFragment"/>
                          </navigation>


                          And use it with your navigation controller to navigate with the correct bundle when a user click on a menu item. Be carefull to remove .setupWithNavController(navController) call from your NavigationView, or it will override your behavior.






                          share|improve this answer













                          If anyone is still looking for an answer to this question. I've found a usefull workaround with the NavigationUI Global actions (working with alpha-09)



                          You can provide a top level action to your navigation graph:



                          <?xml version="1.0" encoding="utf-8"?>
                          <navigation xmlns:app="http://schemas.android.com/apk/res-auto"
                          xmlns:tools="http://schemas.android.com/tools"
                          xmlns:android="http://schemas.android.com/apk/res/android"
                          android:id="@+id/main_nav"
                          app:startDestination="@id/mainFragment">
                          ...
                          <action android:id="@+id/action_global_mainFragment"
                          app:destination="@id/mainFragment"/>
                          </navigation>


                          And use it with your navigation controller to navigate with the correct bundle when a user click on a menu item. Be carefull to remove .setupWithNavController(navController) call from your NavigationView, or it will override your behavior.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 22 at 16:15









                          Nicolas PietriNicolas Pietri

                          715




                          715






























                              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%2f53324347%2fhow-do-you-pass-arguments-to-fragment-called-from-navigationdrawer-using-navigat%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

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues