Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Use HomeFragment's viewLifecycleOwner as the lifecycle for accountMan…
Browse files Browse the repository at this point in the history
…ager observer registry

If we just use the HomeFragment itself, we end up with a memory leak since the lifecycle events
that would clean up the registry (e.g. destroy) won't run (if the fragment is retained in the backstack, for example).
  • Loading branch information
Grisha Kruglov committed Apr 17, 2020
1 parent 2056b6f commit 2f2531e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,12 @@ class HomeFragment : Fragment() {
)

requireComponents.backgroundServices.accountManagerAvailableQueue.runIfReadyOrQueue {
// By the time this code runs, we may not be attached to a context.
if ((this@HomeFragment).context == null) {
// By the time this code runs, we may not be attached to a context or have a view lifecycle owner.
if ((this@HomeFragment).view?.context == null) {
return@runIfReadyOrQueue
}
requireComponents.backgroundServices.accountManager.register(currentMode, owner = this@HomeFragment)

requireComponents.backgroundServices.accountManager.register(currentMode, owner = this@HomeFragment.viewLifecycleOwner)
requireComponents.backgroundServices.accountManager.register(object : AccountObserver {
override fun onAuthenticated(account: OAuthAccount, authType: AuthType) {
if (authType != AuthType.Existing) {
Expand All @@ -398,7 +399,7 @@ class HomeFragment : Fragment() {
}
}
}
}, owner = this@HomeFragment)
}, owner = this@HomeFragment.viewLifecycleOwner)
}

if (context.settings().showPrivateModeContextualFeatureRecommender &&
Expand Down Expand Up @@ -618,7 +619,7 @@ class HomeFragment : Fragment() {

@SuppressWarnings("ComplexMethod", "LongMethod")
private fun createHomeMenu(context: Context, menuButtonView: WeakReference<MenuButton>) = HomeMenu(
this,
this.viewLifecycleOwner,
context,
onItemTapped = {
when (it) {
Expand Down

0 comments on commit 2f2531e

Please sign in to comment.