Search code examples
androidkotlinnavigationandroid-architecture-components

How to Update the previous screen if using navigation component?


I am new to the android architecture navigation component. So I have a screen that shows me a list of the channels I have recently subscribed to and a button that navigates me to another fragment where I could subscribe to other channels.

Now I have the issue since the navigation graph is handling the back press of screens, so I wanted to pass the list of the newly subscribed channel, so that I can show an updated channel on the previous screen 'Recently Subscription' tab. List of channels


Solution

  • make a shared ViewModel to communicate between all your fragments in the navigation graph. all your fragments in the nav graph should have access to this shared viewModel. so you can have a list of 'Recently Subscription' channels in it and update the list in a screen that you need to and use it in any screens you want

    Update: use this links if you need to see some examples:

    Share data between fragments

    Shared ViewModel in Android: Shared between fragments