How to implement a single FAB across numerous fragments?

1.4k views Asked by At

I'm interested in implementing a FloatingActionButton or FAB across a number of fragments. When the fragment changes, the FAB's icon should change and the button should obviously then do something pertaining to that specific fragment, however for a seamless display, the FAB is located within the Main Activity and not per-fragment.

What's the most proper and/or efficient way of doing this?

I can't wrap my head around it.. I've tried to implement listeners in the Fragment, for the click in the Activity, but it's saying the listeners are Cyclical

Thanks

1

There are 1 answers

2
JF YOBOUE On

You can get the Floating Action Button in your fragments e.g in onCreate method of fragment by doing something that

FloatingActionButton fab = (FloatingActionButton)this.getActivity().findViewById(R.id.fab);