1
0
Fork 0

Use HomeFragment's viewLifecycleOwner as the lifecycle for accountManager 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).
master
Grisha Kruglov 2020-04-16 18:06:57 -07:00 committed by Emily Kager
parent 72fe9fad0c
commit 0ec6d266e8
1 changed files with 9 additions and 5 deletions

View File

@ -379,11 +379,15 @@ 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) {
@ -398,7 +402,7 @@ class HomeFragment : Fragment() {
}
}
}
}, owner = this@HomeFragment)
}, owner = this@HomeFragment.viewLifecycleOwner)
}
if (context.settings().showPrivateModeContextualFeatureRecommender &&
@ -618,7 +622,7 @@ class HomeFragment : Fragment() {
@SuppressWarnings("ComplexMethod", "LongMethod")
private fun createHomeMenu(context: Context, menuButtonView: WeakReference<MenuButton>) = HomeMenu(
this,
this.viewLifecycleOwner,
context,
onItemTapped = {
when (it) {