java - Android / Java:Fragment被销毁时TextInputEditText泄漏

我有一个具有简单布局的Fragment
ConstraintLayout-> LinearLayout-> TextInputLayout-> TextInputEditText

看起来还可以,但是,当我导航到另一个Fragment时,它破坏了当前的TextInputEditText,LeakCanary向我显示了与null有关的意外内存泄漏,即使我将onDestroyView()设置为android.support.design.widget.TextInputEditText cc>。

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
当我从布局中删除时,不会出现泄漏。

最佳答案

这不是解决方案,但可能会带您解决一个问题。

由于您使用的是LeakCanary,请检查其AndroidExcludedRefs.java文件,其中包含Android SDK本身或某些制造商版本中的已知泄漏列表。

它包含InputMethodManager的几个已知的泄漏,其中一个可能是您看到泄漏的原因。两者都包含指向可能的解决方法的“ Hack”链接。

  INPUT_METHOD_MANAGER__SERVED_VIEW(SDK_INT >= ICE_CREAM_SANDWICH_MR1 && SDK_INT <= N_MR1) {
    @Override void add(ExcludedRefs.Builder excluded) {
      String reason = "When we detach a view that receives keyboard input, the InputMethodManager"
          + " leaks a reference to it until a new view asks for keyboard input."
          + " Tracked here: https://code.google.com/p/android/issues/detail?id=171190"
          + " Hack: https://gist.github.com/pyricau/4df64341cc978a7de414";
      excluded.instanceField("android.view.inputmethod.InputMethodManager", "mNextServedView")
          .reason(reason);
      excluded.instanceField("android.view.inputmethod.InputMethodManager", "mServedView")
          .reason(reason);
      excluded.instanceField("android.view.inputmethod.InputMethodManager",
          "mServedInputConnection").reason(reason);
    }
  },

  INPUT_METHOD_MANAGER__ROOT_VIEW(SDK_INT >= ICE_CREAM_SANDWICH_MR1 && SDK_INT <= M) {
    @Override void add(ExcludedRefs.Builder excluded) {
      excluded.instanceField("android.view.inputmethod.InputMethodManager", "mCurRootView")
          .reason("The singleton InputMethodManager is holding a reference to mCurRootView long"
              + " after the activity has been destroyed."
              + " Observed on ICS MR1: https://github.com/square/leakcanary/issues/1"
              + "#issuecomment-100579429"
              + " Hack: https://gist.github.com/pyricau/4df64341cc978a7de414");
    }
  },

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

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

标签 java android android-fragments memory-leaks leakcanary


相关文章:

android - Android中的Facebook SDK

android - Android Facebook API和ShareLinkContent

android - BroadcastReceiver ClassNotFound异常

android - 在片段中调用时,getSystemServices是否未定义?

android - 使用工具栏+ TabLayout充气AppBarLayout

java - 休眠> CLOB> Oracle :(

java - Java中线程的等待和通知序列

java - String#matches中的\ A \ z是否冗余?

java - EJB拦截器与CDI拦截器

android - 屏幕旋转后片段不显示