How to implement “onActivityResult” but for fragments, using the new Navigation from Androidx












0














I just searched the docs about using and implement the new Navigation Component, and I dont see or find a method to send data to previous fragment.










share|improve this question





























    0














    I just searched the docs about using and implement the new Navigation Component, and I dont see or find a method to send data to previous fragment.










    share|improve this question



























      0












      0








      0







      I just searched the docs about using and implement the new Navigation Component, and I dont see or find a method to send data to previous fragment.










      share|improve this question















      I just searched the docs about using and implement the new Navigation Component, and I dont see or find a method to send data to previous fragment.







      android-architecture-components






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 9:08

























      asked Nov 20 '18 at 8:57









      Catluc

      880716




      880716
























          1 Answer
          1






          active

          oldest

          votes


















          0














          There is a feature request for this feature, you can star it:
          https://issuetracker.google.com/issues/79672220.



          But for now you can implement this using LiveData and ViewModel, First fragment will observe some LiveData object inside your ViewModel, second fragment will change this object value, and when you will go back to first fragment it will be notified that the value is changed.






          share|improve this answer





















          • Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
            – Catluc
            Nov 21 '18 at 11:01










          • Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
            – Alex
            Nov 21 '18 at 11:33










          • If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
            – Catluc
            Nov 22 '18 at 10:41










          • There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
            – Alex
            Nov 22 '18 at 12:29











          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%2f53389394%2fhow-to-implement-onactivityresult-but-for-fragments-using-the-new-navigation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          There is a feature request for this feature, you can star it:
          https://issuetracker.google.com/issues/79672220.



          But for now you can implement this using LiveData and ViewModel, First fragment will observe some LiveData object inside your ViewModel, second fragment will change this object value, and when you will go back to first fragment it will be notified that the value is changed.






          share|improve this answer





















          • Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
            – Catluc
            Nov 21 '18 at 11:01










          • Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
            – Alex
            Nov 21 '18 at 11:33










          • If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
            – Catluc
            Nov 22 '18 at 10:41










          • There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
            – Alex
            Nov 22 '18 at 12:29
















          0














          There is a feature request for this feature, you can star it:
          https://issuetracker.google.com/issues/79672220.



          But for now you can implement this using LiveData and ViewModel, First fragment will observe some LiveData object inside your ViewModel, second fragment will change this object value, and when you will go back to first fragment it will be notified that the value is changed.






          share|improve this answer





















          • Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
            – Catluc
            Nov 21 '18 at 11:01










          • Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
            – Alex
            Nov 21 '18 at 11:33










          • If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
            – Catluc
            Nov 22 '18 at 10:41










          • There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
            – Alex
            Nov 22 '18 at 12:29














          0












          0








          0






          There is a feature request for this feature, you can star it:
          https://issuetracker.google.com/issues/79672220.



          But for now you can implement this using LiveData and ViewModel, First fragment will observe some LiveData object inside your ViewModel, second fragment will change this object value, and when you will go back to first fragment it will be notified that the value is changed.






          share|improve this answer












          There is a feature request for this feature, you can star it:
          https://issuetracker.google.com/issues/79672220.



          But for now you can implement this using LiveData and ViewModel, First fragment will observe some LiveData object inside your ViewModel, second fragment will change this object value, and when you will go back to first fragment it will be notified that the value is changed.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 8:53









          Alex

          2,32821321




          2,32821321












          • Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
            – Catluc
            Nov 21 '18 at 11:01










          • Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
            – Alex
            Nov 21 '18 at 11:33










          • If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
            – Catluc
            Nov 22 '18 at 10:41










          • There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
            – Alex
            Nov 22 '18 at 12:29


















          • Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
            – Catluc
            Nov 21 '18 at 11:01










          • Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
            – Alex
            Nov 21 '18 at 11:33










          • If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
            – Catluc
            Nov 22 '18 at 10:41










          • There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
            – Alex
            Nov 22 '18 at 12:29
















          Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
          – Catluc
          Nov 21 '18 at 11:01




          Correct me, but i think u messed up things. 1st Fragment and 2nd Fragment need to have some Viewodel shared, but on the same activityScope, and not in neither of Fragments, because the viewModel of Fragment will be destroyed when the fragment is destroyed. developer.android.com/topic/libraries/architecture/… This leads to have a lot of trash in the activity in a large app, with single activity
          – Catluc
          Nov 21 '18 at 11:01












          Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
          – Alex
          Nov 21 '18 at 11:33




          Yes you will need to have viewmodel with activity scope, so its data can be shared between fragments. How having viewmodel with activity scope leads to a lot of trash in activity?
          – Alex
          Nov 21 '18 at 11:33












          If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
          – Catluc
          Nov 22 '18 at 10:41




          If i did understand correct, Androidx way of doing things is to have one activity (NavHost), and multiple fragments inside it. So imagine a lot of fragments sharing data beetwen one activity viewModel. That activity viewModel will be very caotic and it will end up as a container. I understand that is possible for the activity to have more than 1 viewmodel associatted, and this will clean things up, but it still dont taste well
          – Catluc
          Nov 22 '18 at 10:41












          There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
          – Alex
          Nov 22 '18 at 12:29




          There is no problem using multiple viewmodels with activity scope, and it is very clean and good solution. And for now i think it is the best solution to implement "onActivityResult" for destinations in navigation architecture component.
          – Alex
          Nov 22 '18 at 12:29


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53389394%2fhow-to-implement-onactivityresult-but-for-fragments-using-the-new-navigation%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

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]