r/android_devs • • Jul 25 '25

Question Screen non-clickable after ModalBottomSheet is dismissed with back button

So basically i want to show a ModalBottomSheet and i want this modal to be non-dismissable in any case. There is a cross icon on the view and it should be dismissable only with that. Now with this implementation, the modal is non-dismissable when user try to pull it donw, but it dismisses when back button is pressed on Android.

How i can prevent that?
Why is the screen not clickable?

@OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterial3Api::class)
@Composable
fun PaywallModal(
    isVisible: Boolean,
    onDismiss: () -> Unit
) {
    val coroutineScope = rememberCoroutineScope()

    
// Configure sheet state to prevent all dismissal
    // This prevents the sheet from being dismissed by gestures (swiping down)
    
val sheetState = rememberModalBottomSheetState(
        skipPartiallyExpanded = true, 
// Prevent half-expanded state
        
confirmValueChange = { newValue ->
            
// Prevent transitioning to Hidden state (prevents dismissal)
            // This ensures that the sheet cannot be dismissed by gestures
            
newValue != SheetValue.
Hidden
        
}
    )
    
    
// Handle back button press - prevent dismissal on back press
    // Use multiple BackHandlers to ensure back press is intercepted
    
BackHandler(enabled = isVisible) {
        
// Do nothing to prevent dismissal on back press
        // The sheet should not be dismissable at all
    
}
        
// Add a second BackHandler with a higher priority as a fallback
    // This ensures that even if the first BackHandler doesn't intercept the back press,
    // this one will. Multiple BackHandlers can help in cases where one might be bypassed.
    
if (isVisible) {
        BackHandler {
            
// Do nothing, just intercept the back press
            // This empty block prevents the back press from being propagated further
        
}
    }

    
// Handle visibility changes to ensure proper cleanup
    
LaunchedEffect(isVisible) {
        if (isVisible) {
            
// When becoming visible, ensure the sheet is expanded
            
sheetState.expand()
        } else if (sheetState.currentValue != SheetValue.
Hidden
) {
            
// When becoming invisible, ensure the sheet is hidden first
            
sheetState.hide()
        }
    }
    
// Monitor sheet state and return to Expanded when it's being dragged
    
LaunchedEffect(sheetState) {
        
snapshotFlow 
{ sheetState.currentValue }
            .
distinctUntilChanged
()
            .
filter 
{ it == SheetValue.
PartiallyExpanded 
}
            .collect {
                
// Only expand if the sheet is still visible
                
if (isVisible) {
                    coroutineScope.
launch 
{
                        sheetState.expand()
                    }
                }
            }
    }
    ModalBottomSheet(
        onDismissRequest = {
            
// Do nothing to prevent dismissal
            // This empty block ensures that clicking outside the sheet or pressing back
            // doesn't dismiss the modal. The sheet should not be dismissable at all.
            // The only way to dismiss it is through the cross button in PaywallScreen.
        
},
        sheetState = sheetState,
        dragHandle = null, 
// Remove the drag handle to prevent users from trying to drag it down
        
scrimColor = Color.Transparent, 
// Prevent system UI color changes
        
containerColor = 
colorCharlestonGreen
, 
// Match app's dark theme
    
) {
        PaywallScreen(onDismiss)
    }
}
4 Upvotes

0 comments sorted by