Android FloatingActionButton导致内存泄漏

时间:2019-07-12 16:18:10

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

我在这里看到了几个内存泄漏的帖子,我认为这有点不同。我的片段中有一个floatingactionbutton,每当我使用Navigation graph导航到另一个片段时,fab都报告leakcanary泄漏是我的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:layout_width="match_parent"
                    android:id="@+id/layoutCategories"
                    android:layout_height="wrap_content"
                    android:inflatedId="@+id/panel_layoutCategories"
                    android:layout="@layout/store_layout_categories" />


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


                <ViewStub
                    android:id="@+id/layoutCollections"
                    android:layout_height="wrap_content"
                    android:inflatedId="@+id/panel_layoutCollections"
                    android:layout_width="match_parent"
                    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" />

btnGoToCart是有问题的fab

我的片段看起来像

public class StoreFragment extends Fragment implements View.OnClickListener {


@BindView(R.id.btnGoToCart)
ExtendedFloatingActionButton btnGoToCart;
private StoreFragmentViewModel storeFragmentViewModel;





public StoreFragment() {
    // Required empty public constructor
}


@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);

    loadData();
}


private void loadData() {
    disposables.add(SharedPreferencesUtils
            .getInstance(getContext())
            .isLoggedIn()
            .subscribeWith(new DisposableSingleObserver<Boolean>() {
                @Override
                public void onSuccess(Boolean aBoolean) {
                    if (aBoolean)
                        storeFragmentViewModel.fetchProductCategories();
                }

                @Override
                public void onError(Throwable e) {

                }
            }));
}


@Override
public void onStart() {
    super.onStart();

}



@Override
public void onStop() {
    super.onStop();
    //I tried this hoping it would work nothing
    btnGoToCart.clearAnimation();
    ((ViewGroup) btnGoToCart.getParent()).removeView(btnGoToCart);
    btnGoToCart = null;
}


@Override
public void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
}

}

我尝试在onStop

中这样做
     //I tried this hoping it would work nothing
    btnGoToCart.clearAnimation();
    ((ViewGroup) btnGoToCart.getParent()).removeView(btnGoToCart);
    btnGoToCart = null;

但这没用

下面是Leak canary的摘录 enter image description here

1 个答案:

答案 0 :(得分:1)

您可以共享ExtendedFloatingActionButton实现吗?也许您在那做错了。

如果您在片段中使用ButterKnife,也应该使用:

private Unbinder unbinder;

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    unbinder = ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
}

@Override public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
}
  

BINDING RESET片段的视图生命周期与   活动。在onCreateView中绑定片段时,将视图设置为   在onDestroyView中为null。黄油刀在以下情况下返回Unbinder实例   您调用绑定为您执行此操作。在中调用其unbind方法   适当的生命周期回调。