Skip to content

Instantly share code, notes, and snippets.

@ktvipin27
Last active May 29, 2020 03:35
Show Gist options
  • Save ktvipin27/31dc38f6bf16b33051f2cd0639764af5 to your computer and use it in GitHub Desktop.
Save ktvipin27/31dc38f6bf16b33051f2cd0639764af5 to your computer and use it in GitHub Desktop.
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