Skip to content

Instantly share code, notes, and snippets.

@raygervais
Created January 22, 2019 01:01
Show Gist options
  • Select an option

  • Save raygervais/886203fb9bf9ea6a3cf6fd6993bcc88b to your computer and use it in GitHub Desktop.

Select an option

Save raygervais/886203fb9bf9ea6a3cf6fd6993bcc88b to your computer and use it in GitHub Desktop.
[Overflow Menu in OnCreateView]
// Context: https://github.com/raygervais/kotlin-for-android-course/blob/master/Lesson-3/app/src/main/java/com/example/android/navigation/TitleFragment.kt
class TitleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding: FragmentTitleBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_title, container, false)
binding.playButton.setOnClickListener { view: View ->
view.findNavController().navigate(TitleFragmentDirections.actionTitleFragmentToGameFragment())
}
(activity as AppCompatActivity).supportActionBar?.title = getString(R.string.app_name)
setHasOptionsMenu(true)
return binding.root
}
// Overflow Menu Handling
override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
super.onCreateOptionsMenu(menu, inflater)
inflater?.inflate(R.menu.overflow_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return NavigationUI.onNavDestinationSelected(item!!,
view!!.findNavController()) || super.onOptionsItemSelected(item)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment