Skip to content

Commit

Permalink
Avoid possible memory leaks by calling "onDestroy" manually when back…
Browse files Browse the repository at this point in the history
… button is pressed
  • Loading branch information
Javinator9889 committed May 1, 2020
1 parent 080dae3 commit e5a814b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Expand Up @@ -125,9 +125,14 @@ class MainActivity : ActionBarBase(),
menu.selectedItemId = R.id.diseases
onNavigationItemSelected(menu.menu.findItem(R.id.diseases))
} else {
if (activeFragment == R.id.diseases)
if (activeFragment == R.id.diseases) {
with(fragments[activeFragment].get()!! as DiseasesFragment) {
onBackPressed()
}
fragments.clear()
super.onBackPressed()
else {
finish()
} else {
val washingHandsFragment = fragments[activeFragment].get()
?: createFragmentForId(R.id.handwashing)
as WashingHandsFragment
Expand Down
Expand Up @@ -43,6 +43,7 @@ import com.mikepenz.fastadapter.listeners.ClickEventHook
import kotlinx.android.synthetic.main.diseases_list.*
import kotlinx.android.synthetic.main.diseases_list.view.*
import kotlinx.coroutines.launch
import timber.log.Timber

class DiseasesFragment : BaseFragmentView() {
override val layoutId: Int = R.layout.diseases_list
Expand Down Expand Up @@ -93,6 +94,23 @@ class DiseasesFragment : BaseFragmentView() {
adapter = fastAdapter
}
fastAdapter.addEventHook(DiseaseClickEventHook())
fastAdapter.withSavedInstanceState(savedInstanceState)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
fastAdapter.saveInstanceState(outState)
}

fun onBackPressed() {
try {
diseasesContainer.adapter = null
diseasesAdapter.clear()
} catch (e: Exception) {
Timber.w(e, "Exception when calling 'onBackPressed'")
} finally {
onDestroy()
}
}

private inner class DiseaseClickEventHook : ClickEventHook<Disease>() {
Expand Down

0 comments on commit e5a814b

Please sign in to comment.