Search code examples
androidkotlinandroidxandroid-architecture-navigation

How to navigate Fragment from notification in Android Navigation Controller


Should i make a Intent that leads to the MainActivity and then navigate function there. or is there another way?


Solution

  • From notification write pending intent to MainActivity and from there navigate to specific fragment using its id

    val bundle = intent.extras
    findNavController(R.id.homeHostFragment)
                .navigate(R.id.notificationsFragment, bundle, null)
    

    or

        //here homeHostFragment is the id for fragment in xml for which we set the nav graph
        val bundle = intent.extras
        val navHostFragment = homeHostFragment as NavHostFragment
        navHostFragment.navController.navigate(R.id.notificationsFragment,bundle)