浮动操作按钮导致内存泄漏

时间:2019-07-16 14:30:07

标签: android memory-leaks floating-action-button

我正在处理一个应用程序,该应用程序正在使用leak canary检测possible memory leaks。除了我的extended floating action button以外,该应用程序似乎还不错,根据leak canary的说法,该按钮正在泄漏,我不知道如何解决此问题。

下面是我的XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.store.StoreFragment">


    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/small_margin"
        app:cardElevation="@dimen/normal_elevation"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlayLargeCutLeftTopCorner">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipeToRefresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <androidx.core.widget.NestedScrollView
                android:id="@+id/nestScrollView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fadingEdge="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">


                    <ViewStub
                        android:id="@+id/layoutCategories"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inflatedId="@+id/panel_layoutCategories"
                        android:layout="@layout/store_layout_categories" />


                    <ViewStub
                        android:id="@+id/layoutDeals"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inflatedId="@+id/panel_layoutDeals"
                        android:layout="@layout/store_layout_deals" />


                    <ViewStub
                        android:id="@+id/layoutCollections"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inflatedId="@+id/panel_layoutCollections"
                        android:layout="@layout/store_layout_collections" />
                </LinearLayout>

            </androidx.core.widget.NestedScrollView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/btnGoToCart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:text="@string/cart"
        android:textColor="@android:color/white"
        android:textStyle="bold"
        app:backgroundTint="@color/colorAccent"
        app:elevation="@dimen/large_elevation"
        app:icon="@drawable/ic_shopping_cart_24px"
        app:iconTint="@android:color/white"
        app:layout_anchor="@id/nestScrollView"
        app:layout_anchorGravity="bottom|end"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlayLargeCutLeftTopCorner" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

还有我的Java代码


public class StoreFragment extends Fragment implements View.OnClickListener {

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_store, container, false);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
  btnGoToCart.setOnClickListener(this);
     }



  @Override
    public void onClick(View v) {

        switch (v.getId()) {
                ....
            case R.id.btnGoToCart:
                Navigation.findNavController(v).navigate(R.id.cartFragment);
                break;
                ....
                }
              }
}

下面是泄漏金丝雀的摘要。起初leak指向coordinator layout的ID,我将其删除,现在它指向Extended floating action buttonenter image description here

1 个答案:

答案 0 :(得分:0)

为什么不让Fragment实现OnClickListener,为什么不创建一个新的OnClickListner内部类呢?从我看到的内容来看,您是将片段作为OnClickListener传递的,而该视图后来持有对该片段的引用,这可能是造成泄漏的原因。只需(new OnClickListener(){});而不是实施OnClickListener。