-
Notifications
You must be signed in to change notification settings - Fork 41
bug: bottom dialog fragment attached not attached to activity #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: bottom dialog fragment attached not attached to activity #117
Conversation
When HomeFragment is destroyed
When setting its onShowListener
WalkthroughTwo 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this 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 caseNulling the
peersBtnclick listener beforebinding = nullis a nice lifecycle/leak safeguard. To be extra defensive against any edge cases wherebindingmight already be cleared beforeonDestroyViewruns, 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 referenceThe new
getActivity() == null || !isAdded()guard is a solid fix to avoidIllegalStateExceptionwhen the fragment is detached beforeonShowruns.As a small follow‑up, you could cache the
Activityafter the check and reuse it instead of callingrequireActivity()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
📒 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)
Summary by CodeRabbit
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.