I have created an abstract BaseFragment
class which will be extended by other concrete Fragment
classes. I want to inject ViewModel
in my BaseFragment
using Koin
. Here is my BaseFragment:
abstract class BaseFragment<out VM : BaseViewModel, DB : ViewDataBinding>(private val mViewModelClass: Class<VM>) : Fragment() {
val viewModel: VM by viewModel()
open lateinit var binding: DB
fun init(inflater: LayoutInflater, container: ViewGroup) {
binding = DataBindingUtil.inflate(inflater, getLayoutRes(), container, false)
}
open fun init() {}
@LayoutRes
abstract fun getLayoutRes(): Int
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
init(inflater, container!!)
init()
super.onCreateView(inflater, container, savedInstanceState)
return binding.root
}
open fun refresh() {}
}
But I am not able to do so. I am using 2.0.1
version of Koin.
I have the same scenario in my case. You can also do like below:
Add your ViewModel as an abstract and set value when you extend your BaseFragment.
My BaseFragment have:
abstract class BaseFragment<Binding : ViewDataBinding, ViewModel : BaseViewModel> : Fragment() {
protected abstract val mViewModel: ViewModel
protected lateinit var bindingObject: Binding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
bindingObject = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false)
return bindingObject.root
}
/**
* Get layout resource id which inflate in onCreateView.
*/
@LayoutRes
abstract fun getLayoutResId(): Int
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
doDataBinding()
}
/**
* Do your other stuff in init after binding layout.
*/
abstract fun init()
private fun doDataBinding() {
bindingObject.lifecycleOwner = viewLifecycleOwner // it is extra if you want to set life cycle owner in binding
// Here your viewModel and binding variable imlementation
bindingObject.setVariable(BR.viewModel, mViewModel) // In all layout the variable name should be "viewModel"
bindingObject.executePendingBindings()
init()
}
}
Here is my actual Fragment implementation:
class FragmentComments : BaseFragment<FragmentCommentsBinding, FragmentCommentsVM>() {
// Here is the your viewmodel imlementation
override val mViewModel: FragmentCommentsVM by viewModel()
override fun getLayoutResId(): Int = [fragment layout id like "R.layout.fragment_com"]
override fun init() {
...
}
I hope this helps you. Let me know if more help required!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With