Skip to content

Instantly share code, notes, and snippets.

@wellingtoncosta
Created January 16, 2019 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellingtoncosta/0465344f967e80c3c8e9c33c48c78e66 to your computer and use it in GitHub Desktop.
Save wellingtoncosta/0465344f967e80c3c8e9c33c48c78e66 to your computer and use it in GitHub Desktop.
class AwesomeFragment : Fragment() {
private var callback: CallbackListener? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_awesome, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if(activity is CallbackListener) {
callback = activity
}
}
override fun onViewCreated(view: View, savedInstance: Bundle?) {
super.onViewCreated(view, savedInstance)
val button = view.findViewById<Button>(R.id.my_button)
button.setOnClickListener { callback?.let { it.onCallback() } }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment