From 86a470a5b550d75aae6149b110eb590c046a2e48 Mon Sep 17 00:00:00 2001 From: Serchinastico <54cymru@gmail.com> Date: Tue, 29 Jan 2019 16:59:43 +0100 Subject: [PATCH] Dispose observer in onDestroy. Make the lifecycle subscription generic on BaseActivity --- .../karumi/jetpack/superheroes/ui/view/BaseActivity.kt | 8 ++++++++ .../jetpack/superheroes/ui/view/EditSuperHeroActivity.kt | 3 +-- .../karumi/jetpack/superheroes/ui/view/MainActivity.kt | 3 +-- .../superheroes/ui/view/SuperHeroDetailActivity.kt | 3 +-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt index 5e0adf6..8b2508d 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/BaseActivity.kt @@ -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 @@ -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?) {} } \ No newline at end of file diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt index 177ac2a..2dec268 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/EditSuperHeroActivity.kt @@ -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 @@ -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() ?: "", diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt index 8c909be..d33c91d 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/MainActivity.kt @@ -17,7 +17,7 @@ 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 @@ -25,7 +25,6 @@ class MainActivity : BaseActivity(), SuperHeroesPresenter.View { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - lifecycle.addObserver(presenter) initializeAdapter() initializeRecyclerView() } diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt index 718ddbe..76f423c 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/ui/view/SuperHeroDetailActivity.kt @@ -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 @@ -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,