【发布时间】:2018-12-15 11:43:15
【问题描述】:
我有一个Fragment,布局简单:ConstraintLayout -> LinearLayout -> TextInputLayout -> TextInputEditText
它看起来不错并且可以工作,但是当我导航到另一个 Fragment 时,这意味着当前的 Fragment 被破坏,LeakCanary向我显示了一个意想不到的内存泄漏,与TextInputEditText 相关,即使我在onDestroyView() 中将其设置为null。
LeakCanary 报告:
ConstraintLayout leaked
-> InputMethodManager$ControlledInputConnectionWrapper.mInputConnection
-> EditableInputConnection.mTextView
-> TextInputEditText.mParent
MyFragment.java
public class MyFragment extends Fragment {
private View view;
private EditText et;
public MyFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.my_layout, container, false);
et = view.findViewById(R.id.my_et);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
et = null;
view = null;
// No difference, if I call super.onDestroyView(); here
}
}
my_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_cl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="@+id/my_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="numberPassword" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
有什么想法吗?提前致谢!
编辑 1
当我从布局中删除 android.support.design.widget.TextInputEditText 时,泄漏不会出现。
【问题讨论】:
-
尝试在
super()之前将其设置为null? -
@MeowCat2012 感谢您的回答,但它没有改变任何东西。我给
onDestroyView()添加了评论。
标签: java android android-fragments memory-leaks leakcanary