Search code examples
androidandroid-fragmentsandroid-architecture-navigation

How to refresh the content of the fragments with the navigation component when returning from an activity?


I currently have an activity that implements a bottom nav bar with the navigation component to navigate between 4 fragments.

Right now, one of these fragments opens a new activity and I would like when closing it to refresh the fragment content again, as this happens only when I navigate between the fragments.

Any idea how to do this? Thanks in advance.


Solution

  • Finally, I coded the following solution which worked perfectly. I hope it can be useful to someone, best regards!

    private fun refreshFragment() {
        navController.currentDestination?.id?.let { id ->
            navController.popBackStack(id, inclusive = true)
            navController.navigate(id)
        }
    }
    
    override fun onResume() {
        super.onResume()
        refreshCurrentFragment()
    }