Skip to content

Instantly share code, notes, and snippets.

@Chullian
Last active June 23, 2019 19:13
Show Gist options
  • Select an option

  • Save Chullian/16a1df87aefb3a7c7326edabca512f89 to your computer and use it in GitHub Desktop.

Select an option

Save Chullian/16a1df87aefb3a7c7326edabca512f89 to your computer and use it in GitHub Desktop.

Revisions

  1. Chullian revised this gist Jun 23, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion BaseActivity
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    abstract class BaseActivity<T : BaseViewModel> : FragmentActivity(), BaseView {
    abstract class BaseActivity<T : BaseViewModel> : AppCompatActivity(), BaseView {

    protected abstract val layoutId: Int

  2. Chullian created this gist Jun 23, 2019.
    54 changes: 54 additions & 0 deletions BaseActivity
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    abstract class BaseActivity<T : BaseViewModel> : FragmentActivity(), 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
    }