title: Retrieve data from previous instance with navigation controller
body: <p>I'm kinda new to Android developpement and I use the 'Navigation' library.</p>
  <p>Starting from my first fragment (which is a recyclerview that fetch data from an API), if I navigate to another fragment, the navigation controller destroy the first fragment and create the second fragment and show it. If I want to return to the first one (with the left arrow or the back button), it destroy de second fragment and create the first from scratch, making it reload all the data and using bandwith.</p>
  <p>I have read many solutions for this but they are all fastidious :</p>
  <ul>
  <li>using mvvm</li>
  <li>write my own navigation controller</li>
  <li>using mvp</li>
  </ul>
  <p>I'd like to know what's the better way to retrieve data back without calling again my API.</p>
  <p>My first fragment :</p>
  <pre><code>public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          AnnoncesViewModel annoncesViewModel = new ViewModelProvider(this).get(AnnoncesViewModel.class);
          root = inflater.inflate(R.layout.fragment_annonces, container, false);
          ctx = root.getContext();
  
          recyclerView = root.findViewById(R.id.listeannonce_rv);
  
          annoncesViewModel.getAnnonces().observe(this, data-&gt; {
              recyclerViewAdapter = new ListeAnnoncesAdapter(data, ctx, AnnoncesFragment.this);
              recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext()));
              recyclerView.setAdapter(recyclerViewAdapter);
          });
  
          return root;
      }
  </code></pre>
  <p>The viewmodel :</p>
  <pre><code>public class AnnoncesViewModel extends ViewModel {
  
      MutableLiveData&lt;ArrayList&lt;Annonce&gt;&gt; annonces;
      ArrayList&lt;Annonce&gt; AnnonceArrayList;
  
      public AnnoncesViewModel() {
          annonces = new MutableLiveData&lt;&gt;();
          AnnonceArrayList = new ArrayList&lt;&gt;();
          annonces.setValue(AnnonceArrayList);
      }
  
      public MutableLiveData&lt;ArrayList&lt;Annonce&gt;&gt; getAnnonces() {
          return annonces;
      }
  }
  </code></pre>
  <p>For navigation, i use</p>
  <pre><code>navController.navigate(R.id.frag1_to_frag2);
  </code></pre>
  <p>or</p>
  <pre><code>navController.navigate(R.id.nav_frag2);
  </code></pre>
  <p>But it doesn't change anything.</p>
  <p>At the moment, the data is retrieved when I press a button.</p>
  <p>Thanks for help !</p>