android - 片段RootViews和/或数据绑定变量泄漏

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

但是我的片段根视图和/或数据绑定变量在此过程中泄漏。

请参阅下面的LeakCanary分析。 clFeedFragmentRootFragmentMainFeed的根视图,

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(绑定包含对视图的引用,因此您需要在onDestroyView()之后放开绑定对象

本文翻译自 https://stackoverflow.com/questions/58201827/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

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


相关文章:

android - Repository方法在Asynchronous Retrofit调用中设置LiveData值

android - 无法从SQLiteCursorLoader转换为Loader <Cursor>

android - 将多个参数放入ContentResolver.requestSync

android - 使用Android体系结构组件将单向数据绑定转换为双向数据

android - 具有数据绑定功能的Android分页库

android - 直接实例化ViewModels,而不使用ViewModelProviders.of方法

android - 从Android应用程序拨打号码,而无需使用本地操作呼叫意图?

android - android-screenshot-library的示例项目

android - 为什么我应该使用实时数据而不是可观察的数据?

java - Android Bindings停止工作