我的对话框片段保持内存泄漏,尽管我已经设置了binding = null
覆盖onDestroyView.这里是我的onCreateView和onDestroyView:
@AndroidEntryPoint
class AddressDialogFragment : BottomSheetDialogFragment() {
private var binding: FragmentAddressDialogBinding? = null
private val viewModel: AddressViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding =
DataBindingUtil.inflate(inflater, R.layout.fragment_address_dialog, container, false)
binding?.viewModel = viewModel
binding?.lifecycleOwner = this.viewLifecycleOwner
// doing my things
val view = binding?.root
return view!!
}
}
override fun onDestroyView() {
binding = null
super.onDestroyView()
}
我使用导航组件导航到此DiaLogFragment,但不认为这是一个问题。我使用了binding.unbind()
或dismiss()
DialogFragmet,但它没有任何用处。我还检查了DialogFragment是否已调用onDestroyView和onDestroy。
这是我从LeakCanary获得的泄漏(1次打开和关闭对话框2次泄漏,但只有1次明显泄漏):
│ GC Root: System class
│
├─ android.view.accessibility.AccessibilityManager class
│ Leaking: NO (a class is never leaking)
│ ↓ static AccessibilityManager.sInstance
│ ~~~~~~~~~
├─ android.view.accessibility.AccessibilityManager instance
│ Leaking: UNKNOWN
│ Retaining 374.7 kB in 8787 objects
│ ↓ AccessibilityManager.mTouchExplorationStateChangeListeners
│ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
├─ android.util.ArrayMap instance
│ Leaking: UNKNOWN
│ Retaining 374.4 kB in 8775 objects
│ ↓ ArrayMap.mArray
│ ~~~~~~
├─ java.lang.Object[] array
│ Leaking: UNKNOWN
│ Retaining 374.4 kB in 8773 objects
│ ↓ Object[0]
│ ~~~
├─ com.google.android.material.textfield.DropdownMenuEndIconDelegate$7 instance
│ Leaking: UNKNOWN
│ Retaining 14.9 kB in 264 objects
│ Anonymous class implementing android.view.accessibility.AccessibilityManager$TouchExplorationStateChangeListener
│ ↓ DropdownMenuEndIconDelegate$7.this$0
│ ~~~~~~
├─ com.google.android.material.textfield.DropdownMenuEndIconDelegate instance
│ Leaking: UNKNOWN
│ Retaining 14.9 kB in 263 objects
│ context instance of androidx.appcompat.view.ContextThemeWrapper, wrapping activity com.example.aposs_buyer.
│ uicontroler.activity.AddressActivity with mDestroyed = false
│ ↓ EndIconDelegate.textInputLayout
│ ~~~~~~~~~~~~~~~
├─ com.google.android.material.textfield.TextInputLayout instance
│ Leaking: YES (View detached yet still part of window view hierarchy)
│ Retaining 12.4 kB in 298 objects
│ View is part of a window view hierarchy
│ View.mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ mContext instance of androidx.appcompat.view.ContextThemeWrapper, wrapping activity com.example.aposs_buyer.
│ uicontroler.activity.AddressActivity with mDestroyed = false
│ ↓ View.mParent
├─ android.widget.LinearLayout instance
│ Leaking: YES (TextInputLayout↑ is leaking and View detached yet still part of window view hierarchy)
│ Retaining 157.8 kB in 3879 objects
│ View is part of a window view hierarchy
│ View.mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ mContext instance of dagger.hilt.android.internal.managers.ViewComponentManager$FragmentContextWrapper, wrapping
│ activity com.example.aposs_buyer.uicontroler.activity.AddressActivity with mDestroyed = false
│ ↓ View.mParent
╰→ androidx.core.widget.NestedScrollView instance
Leaking: YES (ObjectWatcher was watching this because com.example.aposs_buyer.uicontroler.fragment.
AddressDialogFragment received Fragment#onDestroyView() callback (references to its views should be cleared to
prevent leaks) and View detached yet still part of window view hierarchy)
Retaining 39.2 kB in 782 objects
key = 3588963a-7778-4442-ac60-a99f5380ede0
watchDurationMillis = 281964
retainedDurationMillis = 276959
View is part of a window view hierarchy
View.mAttachInfo is null (view detached)
View.mWindowAttachCount = 1
mContext instance of dagger.hilt.android.internal.managers.ViewComponentManager$FragmentContextWrapper, wrapping
activity com.example.aposs_buyer.uicontroler.activity.AddressActivity with mDestroyed = false
我希望我很快就能得到答案。我已经为这个漏洞坚持了两天了。
2条答案
按热度按时间ljo96ir51#
我刚刚发现是材质设计库的TextInputLayout的漏洞。我需要做的就是将材质设计从1.6.0更新到1.7.0
implementation 'com.google.android.material:material:1.7.0'
。相关链接可以帮助我解决这个问题。https://github.com/material-components/material-components-android/issues/26156ojccjat2#
此处您应在
onPause()
或onDestroy()
方法中调用dismiss()
方法。根据您的要求,否则它将在bottomSheetFragment的示例保持活动状态时创建内存泄漏。