Skip to content

Instantly share code, notes, and snippets.

@ktvipin27
Last active May 29, 2020
Embed
What would you like to do?
BaseActivity for the demo of template creation.
abstract class BaseActivity<D : ViewDataBinding, V : BaseViewModel> : AppCompatActivity(){
@get:LayoutRes
protected abstract val layoutId: Int
protected abstract val viewModelClass: Class<V>
internal val viewModel:V by lazy { ViewModelProvider(this).get(viewModelClass) }
internal lateinit var binding: D
private set
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.onActivityCreated(intent.extras)
binding = DataBindingUtil.setContentView(this, layoutId)
binding.setVariable(BR.viewModel, viewModel)
binding.lifecycleOwner = this
onCreated(savedInstanceState,intent.extras)
}
open fun onCreated(savedInstanceState: Bundle?, extras: Bundle?) { }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
viewModel.onActivityResult(requestCode, resultCode, data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment