Skip to content

Instantly share code, notes, and snippets.

@kakai248
Created February 26, 2020 15:04
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 kakai248/4c743a78f16004e419bf737bd26c891e to your computer and use it in GitHub Desktop.
Save kakai248/4c743a78f16004e419bf737bd26c891e to your computer and use it in GitHub Desktop.
abstract class BaseFragment<VB : ViewBinding>(
private val bindingFactory: (LayoutInflater, ViewGroup?, Boolean) -> VB
) : Fragment() {
private var _binding: VB? = null
val binding: VB
get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = bindingFactory(inflater, container, false).also { _binding = it }.root
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
class ExampleFragment : BaseFragment<FragmentExampleBinding>(FragmentExampleBinding::inflate)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment