Skip to content

Conversation

@doromaraujo
Copy link
Collaborator

@doromaraujo doromaraujo commented Dec 1, 2025

Summary by CodeRabbit

Bug Fixes

  • Fixed potential crashes when dialog interactions occur during fragment lifecycle transitions
  • Improved cleanup of UI component references to enhance app stability and prevent memory-related issues

✏️ Tip: You can customize this high-level summary in your review settings.

@doromaraujo doromaraujo requested a review from pappz December 1, 2025 17:52
@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Walkthrough

Two UI fragments receive defensive programming fixes. BottomDialogFragment adds a lifecycle attachment guard to prevent crashes when manipulating the BottomSheet after detachment. HomeFragment removes unused imports and cleans up a button listener reference in onDestroyView to prevent memory leaks.

Changes

Cohort / File(s) Summary
Lifecycle Safety Guard
app/src/main/java/io/netbird/client/ui/home/BottomDialogFragment.java
Adds attachment check (getActivity() != null && isAdded()) in BottomSheet's onShow listener to prevent IllegalStateException when fragment is detached.
Listener Cleanup & Unused Imports
app/src/main/java/io/netbird/client/ui/home/HomeFragment.java
Removes unused imports (AlertDialog, PreferenceUI) and adds null assignment for peers button listener in onDestroyView to eliminate dangling references.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Both changes follow standard Android best practices for lifecycle management and listener cleanup
  • Guard condition is straightforward defensive check
  • No complex logic or control flow modifications

Poem

🐰 A rabbit hops through Android's halls,
Patching guards before the fragment falls,
Listeners pruned with careful care,
No memory leaks shall linger there!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title is partially related to the changeset but contains awkward phrasing ('attached not attached') that obscures clarity. It addresses the main fix (checking fragment attachment before dialog manipulation) but could be more concise and professional. Revise the title to be clearer, such as: 'Fix IllegalStateException in BottomDialogFragment by checking fragment attachment' or 'Prevent IllegalStateException when BottomDialog operates on detached fragment'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch bug/bottom-dialog-fragment-attached-not-attached-to-activity

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
app/src/main/java/io/netbird/client/ui/home/HomeFragment.java (1)

122-130: Listener cleanup is good; consider guarding for a null binding edge case

Nulling the peersBtn click listener before binding = null is a nice lifecycle/leak safeguard. To be extra defensive against any edge cases where binding might already be cleared before onDestroyView runs, you could wrap this access in a null check:

@Override
public void onDestroyView() {
    super.onDestroyView();
    buttonAnimation.destroy();
    stateListenerRegistry.unregisterServiceStateListener(this);

    if (binding != null) {
        FrameLayout openPanelCardView = binding.peersBtn;
        openPanelCardView.setOnClickListener(null);
    }

    binding = null;
}
app/src/main/java/io/netbird/client/ui/home/BottomDialogFragment.java (1)

47-79: Attachment guard avoids crashes; you can also reuse the checked Activity reference

The new getActivity() == null || !isAdded() guard is a solid fix to avoid IllegalStateException when the fragment is detached before onShow runs.

As a small follow‑up, you could cache the Activity after the check and reuse it instead of calling requireActivity() multiple times, which also removes the tiny window between the check and subsequent calls:

 dialog.setOnShowListener(dialogInterface -> {
-    // Check if the fragment is still attached to avoid IllegalStateException
-    if (getActivity() == null || !isAdded()) {
-        return;
-    }
+    // Check if the fragment is still attached to avoid IllegalStateException
+    Activity activity = getActivity();
+    if (activity == null || !isAdded()) {
+        return;
+    }

     FrameLayout bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet);
     if (bottomSheet != null) {
         // Set the bottom sheet to be full screen
         BottomSheetBehavior<View> behavior = BottomSheetBehavior.from(bottomSheet);
@@
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
-            WindowMetrics windowMetrics = requireActivity().getWindowManager().getCurrentWindowMetrics();
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
+            WindowMetrics windowMetrics = activity.getWindowManager().getCurrentWindowMetrics();
@@
-        } else {
+        } else {
             DisplayMetrics displayMetrics = new DisplayMetrics();
-            requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
+            activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

You’d also need:

import android.app.Activity;

near the top of the file.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f9376e3 and 3f1a64e.

📒 Files selected for processing (2)
  • app/src/main/java/io/netbird/client/ui/home/BottomDialogFragment.java (1 hunks)
  • app/src/main/java/io/netbird/client/ui/home/HomeFragment.java (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: build-debug
  • GitHub Check: Analyze (java-kotlin)

@doromaraujo doromaraujo merged commit bf4ece6 into main Dec 1, 2025
7 checks passed
@doromaraujo doromaraujo deleted the bug/bottom-dialog-fragment-attached-not-attached-to-activity branch December 1, 2025 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants