Skip to content

Instantly share code, notes, and snippets.

@AsynctaskCoffee
Created December 16, 2022 14:28
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 AsynctaskCoffee/6df642df24d2b9b82af68c7b349eeedb to your computer and use it in GitHub Desktop.
Save AsynctaskCoffee/6df642df24d2b9b82af68c7b349eeedb to your computer and use it in GitHub Desktop.
BaseFragment Example
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
typealias Inflate<T> = (LayoutInflater, ViewGroup?, Boolean) -> T
abstract class BaseFragment<VB : ViewBinding>(
private val inflate: Inflate<VB>
) : Fragment() {
private var _binding: VB? = null
val binding get() = _binding!!
abstract fun observeViewModel()
abstract fun setupViews(binding: VB)
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//
_binding = inflate.invoke(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
observeViewModel()
setupViews(binding)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment