adrianfc
5/1/2020 - 8:55 AM

Navigation

// NavHostFragment
<fragment
                android:id="@+id/myNavHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:navGraph="@navigation/navigation"
                app:defaultNavHost="true"
          />

// Navigaton (using button)
binding.playButton.setOnClickListener { view: View ->
  Navigation.findNavController(view).navigate(R.id.action_titleFragment_to_gameFragment)
}

// MainActivity.java (correct usage of Up button)
val navController = this.findNavController(R.id.myNavHostFragment)
NavigationUI.setupActionBarWithNavController(this, navController)

override fun onSupportNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.myNavHostFragment)
        return navController.navigateUp()
    }