Skip to content

Commit

Permalink
Dispose observer in onDestroy. Make the lifecycle subscription generi…
Browse files Browse the repository at this point in the history
…c on BaseActivity
  • Loading branch information
Serchinastico committed Jan 29, 2019
1 parent 69a3132 commit 86a470a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.LifecycleObserver
import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
import org.kodein.di.android.closestKodein
Expand All @@ -15,16 +16,23 @@ abstract class BaseActivity : AppCompatActivity(), KodeinAware {
extend(appKodein)
import(activityModules)
}
abstract val presenter: LifecycleObserver
abstract val layoutId: Int
abstract val toolbarView: Toolbar
abstract val activityModules: Kodein.Module

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(presenter)
setContentView(layoutId)
setSupportActionBar(toolbarView)
preparePresenter(intent)
}

override fun onDestroy() {
super.onDestroy()
lifecycle.removeObserver(presenter)
}

open fun preparePresenter(intent: Intent?) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View {
}
}

private val presenter: EditSuperHeroPresenter by instance()
override val presenter: EditSuperHeroPresenter by instance()
override val layoutId = R.layout.edit_super_hero_activity
override val toolbarView: Toolbar
get() = toolbar
Expand All @@ -37,7 +37,6 @@ class EditSuperHeroActivity : BaseActivity(), EditSuperHeroPresenter.View {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(presenter)
bt_save_edition.setOnClickListener {
presenter.onSaveSuperHeroSelected(
name = et_super_hero_name.text?.toString() ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import org.kodein.di.erased.provider

class MainActivity : BaseActivity(), SuperHeroesPresenter.View {

private val presenter: SuperHeroesPresenter by instance()
override val presenter: SuperHeroesPresenter by instance()
private lateinit var adapter: SuperHeroesAdapter
override val layoutId: Int = R.layout.main_activity
override val toolbarView: Toolbar
get() = toolbar

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(presenter)
initializeAdapter()
initializeRecyclerView()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View {
}
}

private val presenter: SuperHeroDetailPresenter by instance()
override val presenter: SuperHeroDetailPresenter by instance()
override val layoutId: Int = R.layout.super_hero_detail_activity
override val toolbarView: Toolbar
get() = toolbar
Expand All @@ -37,7 +37,6 @@ class SuperHeroDetailActivity : BaseActivity(), SuperHeroDetailPresenter.View {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycle.addObserver(presenter)
edit_super_hero.setOnClickListener {
EditSuperHeroActivity.open(
this@SuperHeroDetailActivity,
Expand Down

0 comments on commit 86a470a

Please sign in to comment.