Search code examples
androidandroid-architecture-navigation

Android - How to know, in the host activity, the navigation has come to end


I'm thinking about a new architecture for my application and I'm implementing something new to an app that already exists.

So, basically my new module has a flow, using navigation component, and when this comes to end my app need to know to call the old flow.

How do I know that? Can host activity knows when navigation end?


Solution

  • Your host activity can call addOnDestinationChangedListener so that it is aware of the current fragment in the navigation flow. If my understanding is correct, you could do something like this in your host activity's onCreate:

    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
       @Override
       public void onDestinationChanged(@NonNull NavController controller,
               @NonNull NavDestination destination, @Nullable Bundle arguments) {
           // if you have reached the end of your navigation flow, do something
           if(destination.getId() == R.id.end_dest) {
               // add code calling your old flow here
           } 
       }
    });