com.d4rk.androidtutorials.java icon indicating copy to clipboard operation
com.d4rk.androidtutorials.java copied to clipboard

Memory Leak from Unremoved Click Listeners in onCreateView

Open Mehrad25Software opened this issue 3 months ago • 0 comments

Method: onCreateView() in BottomSheetMenuFragment.java

Description: Click listeners registered in onCreateView() are not properly removed in onDestroyView(), causing memory leaks when the fragment is recreated multiple times.

Steps to Reproduce:

  1. Open the bottom sheet menu multiple times
  2. Close and reopen the fragment repeatedly
  3. Monitor memory usage (increases over time)

Expected Behavior: Listeners should be cleaned up when fragment is destroyed to prevent memory leaks.

Actual Behavior: Listeners remain in memory, causing gradual memory accumulation and potential OutOfMemory errors.

Proposed Fix Clear click listeners before nullifying the binding in onDestroyView():

@Override
public void onDestroyView() {
    super.onDestroyView();
    if (binding != null) {
        binding.menuSettings.setOnClickListener(null);
        binding.menuHelpFeedback.setOnClickListener(null);
        binding.menuUpdates.setOnClickListener(null);
        binding.menuShare.setOnClickListener(null);
        binding = null;
    }
}

Mehrad25Software avatar Nov 11 '25 03:11 Mehrad25Software