title: Navigation component - how to check whether the fragment is newly navigated to vs. poped back to
body: <p>When using the Navigation component, it is simple to navigate to the next fragment and also simple to pop back to the previous fragment.</p>
  <pre><code>Navigation.findNavController(v).navigate(R.id.action1);
  /*vs*/
  Navigation.findNavController(v).popBackStack();
  </code></pre>
  <p>How can I check within <code>onViewCreated()</code> how startDestination / topLevel destination fragments were reached? If it was poped back to I want to run different code compared to when it was newly navigated to.</p>
  <hr />
  <p><strong>Update</strong> thanks to @Thracian for the suggestions:</p>
  <p>I tested:</p>
  <pre><code>navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
          @Override
          public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
              Log.d(&quot;MAIN&quot;, &quot;New destination ID: &quot; + destination.getDisplayName());
          }
      });
  </code></pre>
  <p>But it returns the same result, whether I directly navigate to a fragment or use popBack/navigateUp, so it does not help to distinguish.</p>
  <p>I tested:</p>
  <pre><code>navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
      navHostManager = navHostFragment.getChildFragmentManager();
      navHostManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
          @Override
          public void onBackStackChanged() {
              int backStackEntryCount = navHostManager.getBackStackEntryCount();
              int fragmentCount = navHostManager.getFragments().size();
  
              Log.e(&quot;MAIN&quot;,&quot;backStackEntryCount: &quot;+backStackEntryCount+&quot;, fragmentCount: &quot; + fragmentCount);
          }
      });
  </code></pre>
  <p>Unfortunately the fragment I try to solve this for is the startDestination. Hence the <code>backStackEntryCount</code> is always 0 when I reach it, both for navigateTo and for navigateUp ...</p>
  <p>For my other top-level destinations this does also not properly work, as they always show <code>backStackEntryCount = 1</code> independent of how I reach them.</p>