You can only do it with the abstract method I think. I use a similar thing like the below.
abstract class BaseActivity<V : ViewBinding> : AppCompatActivity() {
protected open var binding: V? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
makeFullScreen()
setContentView(getInflatedLayout(layoutInflater))
setup()
SharePreferenceUtil.setListenerFlutter(this)
}
override fun onDestroy() {
super.onDestroy()
this.binding = null
SharePreferenceUtil.removeListenerFlutter(this)
}
//internal functions
private fun getInflatedLayout(inflater: LayoutInflater): View {
this.binding = setBinding(inflater)
return binding?.root
?: error("Please add your inflated binding class instance")
}
//abstract functions
abstract fun setBinding(layoutInflater: LayoutInflater): V
abstract fun setup()
}
and if you want the fragment abstract you can do like the below
abstract class BaseFragment<T : ViewBinding, A : Any> : Fragment() {
protected var handler: A? = null //It's base activity
protected open var binding: T? = null
override fun onAttach(context: Context) {
super.onAttach(context)
@Suppress("UNCHECKED_CAST")
this.handler = this.activity as? A
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
this.binding = this.setBinding(inflater,container)
return binding!!.root
}
override fun onDestroyView() {
super.onDestroyView()
binding = null
}
abstract fun setBinding(inflater: LayoutInflater, container: ViewGroup?): T
}
and then you set the binding in your fragment;
override fun setBinding(inflater: LayoutInflater, container: ViewGroup?): ActivityMainBinding = ActivityMainBinding.inflate(inflater, container, false)