I have a floating action button (FAB) and an Async Task that calls FAB.hide() before loading some data in background, and FAB.show() after it's done. For some reason, Even though my console log clearly shows a call is always being made to hide() and then show(), sometimes the FAB hides but doesn't show until the task is executed again.
UPDATE
The problem seems to happen if the calls are too close. I actually tried the code:
fab.hide();
fab.show();
and the same problem occurs (fab is not showing). Any ideas for handling this?
UPDATE: As noted on the Issue Tracker, this issue has been fixed as of version 24.2.0 of the support library.
I came across a similar situation. The problem seems to be that the floating action button doesn't consider itself shown or hidden until it's show/hide animation is finished.
So if you call hide();
but then call show();
before the hide animation has finished, then the show animation won't run because the button is still set as being in a shown state already.
I don't know if this is the optimum solution but I solved it as follows:
boolean fabShouldBeShown;
FloatingActionButton.OnVisibilityChangedListener fabListener = new FloatingActionButton.OnVisibilityChangedListener() {
@Override
public void onShown(FloatingActionButton fab) {
super.onShown(fab);
if(!fabShouldBeShown){
fab.hide();
}
}
@Override
public void onHidden(FloatingActionButton fab) {
super.onHidden(fab);
if(fabShouldBeShown){
fab.show();
}
}
};
public void methodWhereFabIsHidden() {
fabShouldBeShown = false;
myFab.hide(fabListener);
}
public void methodWhereFabIsShown() {
fabShouldBeShown = true;
myFab.show(fabListener);
}
By setting our own boolean along with this listener we can deal with these interrupted situations. When we get to the end of the animation we check if we are in the state that we actually want to be, and if not we change to the correct one.
Fixed in support library 24.2.0:
https://code.google.com/p/android/issues/detail?id=216469
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With