Last active
June 23, 2019 19:13
-
-
Save Chullian/16a1df87aefb3a7c7326edabca512f89 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class BaseActivity<T : BaseViewModel> : AppCompatActivity(), BaseView { | |
| protected abstract val layoutId: Int | |
| protected abstract val containerId: Int | |
| protected abstract val viewModelClass: Class<T> | |
| protected val viewModel: T by lazy(LazyThreadSafetyMode.NONE) { ViewModelProviders.of(this).get(viewModelClass) } | |
| abstract fun observeLiveData() | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(layoutId) | |
| observeAllLiveData() | |
| } | |
| private fun observeAllLiveData() { | |
| observeLiveData() | |
| } | |
| override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
| super.onActivityResult(requestCode, resultCode, data) | |
| supportFragmentManager.findFragmentById(containerId)?.onActivityResult(requestCode, resultCode, data) | |
| } | |
| protected open fun replaceFragment(fragment: Fragment, name: String, needToAddToBackStack: Boolean = true) { | |
| hideKeyboard() | |
| with(supportFragmentManager.beginTransaction()) { | |
| replace(containerId, fragment, name) | |
| if (needToAddToBackStack) { | |
| addToBackStack(name) | |
| } | |
| commit() | |
| } | |
| } | |
| protected open fun addFragment(fragment: Fragment, name: String, needToAddToBackStack: Boolean = true) { | |
| hideKeyboard() | |
| with(supportFragmentManager.beginTransaction()) { | |
| add(containerId, fragment, name) | |
| if (needToAddToBackStack) { | |
| addToBackStack(name) | |
| } | |
| commit() | |
| } | |
| } | |
| override fun createViewModelFactory(): ViewModelProvider.NewInstanceFactory? = null | |
| override fun isNeedProgress(): Boolean = true | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment