Disappearing fab icon on navigation fragment change

It's a bug in the FloatingActionButton class: When calling show(), imageMatrixScale is set to 0. A call to setImageResource() then just displays blank. It works before calling show().

The bug has been introduced in the design lib 28.0.0, it was working on v27.1.1. Downgrade to 27.1.1

EDIT: Google Issuetracker


The issue seems to be fixed in Material Components 1.1.0, but it is currently in alpha.

Tried with:

implementation com.google.android.material:material:1.1.0-alpha10


I am having the same issue when the activity goes into onPause then onResume I was calling setImageResource on the FloatingActionButton. The FAB icon was disappearing. My solution was to call the following right after setImageResource

mFloatingActionButton.hide();        
mFloatingActionButton.show();

In my case downgrading design lib wasn't posible for many reasons. MrStahlfelge's answer helped me to find solution:

public class MyNewFab  extends FloatingActionButton {

    private Matrix imageMatrix;

    ...  

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        imageMatrix = getImageMatrix();
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        setImageMatrix(imageMatrix);
    }
}

This works for me. Hope it will help the others facing same problem.