gowong / material-sheet-fab

Android library that provides the floating action button to sheet transition from Google's Material Design.
MIT License
1.58k stars 255 forks source link

fixed memory leak on OnGlobalLayoutListener #22

Closed agrosner closed 8 years ago

agrosner commented 8 years ago

Allows you to remove the OnGlobalLayoutListener from the FAB applied in the MaterialSheetFab so that it doesn't leak the Activity when used in a Fragment.

gowong commented 8 years ago

I wonder if we can solve this without requiring the user to call dispose(). Can you try the following code and see if it solves the memory leak for you? LeakCanary keeps failing to analyze so I'm not able to verify.

fab.getViewTreeObserver()
    .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Removing layout listener to avoid multiple calls
            MaterialSheetFab.this.fab.getViewTreeObserver()
                    .removeGlobalOnLayoutListener(this);
            // Initialize FAB anchor when the FAB view is laid out
            updateFabAnchor();
        }
    });