android - fragment RootViews 和/或数据绑定(bind)变量泄漏

标签 android android-databinding android-architecture-components leakcanary

我正在使用 BottomNavigationView 并在 onNavigationItemSelected 中的 fragment 之间切换,因为我想要自定义进入和退出动画。

但是我的 fragment Root View 和/或数据绑定(bind)变量在此过程中泄漏。

请参阅下面的 LeakCanary 分析。 clFeedFragmentRootFragmentMainFeed

的 Root View

feedBindingFragmentMainFeedBinding

├─ com.google.android.gms.ads.internal.zzq
│    Leaking: NO (zzq↓ is not leaking and a class is never leaking)
│    GC Root: System class
│    ↓ static zzq.zzbmc
├─ com.google.android.gms.ads.internal.zzq
│    Leaking: NO (zzpv↓ is not leaking)
│    ↓ zzq.zzbmk
├─ com.google.android.gms.internal.ads.zzpv
│    Leaking: NO (zzpy↓ is not leaking)
│    ↓ zzpv.zzboy
├─ com.google.android.gms.internal.ads.zzpy
│    Leaking: NO (MainActivity↓ is not leaking)
│    ↓ zzpy.zzzr
├─ com.mypackage.ui.main.MainActivity
│    Leaking: NO (NavHostController↓ is not leaking and Activity#mDestroyed is false)
│    ↓ MainActivity.navController
├─ androidx.navigation.NavHostController
│    Leaking: NO (NavHostFragment↓ is not leaking)
│    ↓ NavHostController.mLifecycleOwner
├─ androidx.navigation.fragment.NavHostFragment
│    Leaking: NO (FragmentManagerImpl↓ is not leaking and Fragment#mFragmentManager is not null)
│    ↓ NavHostFragment.mChildFragmentManager
├─ androidx.fragment.app.FragmentManagerImpl
│    Leaking: NO (HashMap↓ is not leaking)
│    ↓ FragmentManagerImpl.mActive
├─ java.util.HashMap
│    Leaking: NO (HashMap$HashMapEntry[]↓ is not leaking)
│    ↓ HashMap.table
├─ java.util.HashMap$HashMapEntry[]
│    Leaking: NO (HashMap$HashMapEntry↓ is not leaking)
│    ↓ array HashMap$HashMapEntry[].[0]
├─ java.util.HashMap$HashMapEntry
│    Leaking: NO (FragmentStateManager↓ is not leaking)
│    ↓ HashMap$HashMapEntry.value
├─ androidx.fragment.app.FragmentStateManager
│    Leaking: NO (FeedFragment↓ is not leaking)
│    ↓ FragmentStateManager.mFragment
├─ com.mypackage.ui.main.feed.FeedFragment
│    Leaking: NO (Fragment#mFragmentManager is not null)
│    ↓ FeedFragment.feedBinding
│                   ~~~~~~~~~~~
├─ com.mypackage.databinding.FragmentMainFeedBindingImpl
│    Leaking: UNKNOWN
│    ↓ FragmentMainFeedBindingImpl.clFeedFragmentRoot
│                                  ~~~~~~~~~~~~~~~~~~
╰→ androidx.constraintlayout.widget.ConstraintLayout
     Leaking: YES (ObjectWatcher was watching this)
     mContext instance of com.mypackage.ui.main.MainActivity with mDestroyed = false
     View#mParent is null
     View#mAttachInfo is null (view detached)
     View.mWindowAttachCount = 1
     key = 991c9d5b-7065-4c88-9b79-74ad67f5f72d
     watchDurationMillis = 77959
     retainedDurationMillis = 72956
     retainedHeapByteSize=18132), ApplicationLeak(className=androidx.constraintlayout.widget.ConstraintLayout, leakTrace=

我需要帮助解决泄漏问题。 谢谢

最佳答案

在 FeedFragment.onDestroyView() 中,您需要将 FeedFragment.feedBinding 设置为 null(绑定(bind)持有对 View 的引用,因此您需要在 onDestroyView() 之后释放绑定(bind)对象

关于android - fragment RootViews 和/或数据绑定(bind)变量泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201827/

相关文章:

android - 给谷歌地图上色,Android

android - Worker 内部的 Synchronous 或 Asynchronous Rxjava(来自 WorkManager 组件)什么是正确的选择?

android - 在 Android 的 alertdialog builder 中显示 html 文件

java - 从JSON过滤JSON变量

android - lateinit 属性性别尚未初始化

android - kotlin 中数据绑定(bind)的好处?

android - 如何在 Activity 中使用 Room 和 LiveData 删除 LiveData<Object>?

android - 将类转换为Kotlin的子类

android - 不想要我的 TabWidget 上的图标

android - 通过 Data-Binding API 设置一个众所周知的属性值