Problem:
In my compose page -> I have list of elements, if I press anyone I will show sub contents in same page (hide the main content and show the sub content)
If I press back button from topbar, I handle myself by own logic.
But I can't able to handle system back press.
Question :
Is there any way to override the onBackPressed() in Jetpack Compose?
My code:
@Composable
private fun AnimationRootView() {
//Main content list
Column(){
}
//Sub content list
Column(){
if(detail1Clicked)
Detail1()
if(detail2Clicked)
Detail2()
}
....
....
}
Add Modifier. scrollable(..) to the container that you wish to make scrollable. Code would be something like this: val scrollState = rememberScrollState() ... Box( // or whatever your parent composable is modifier = Modifier .
Recomposition is when Jetpack Compose re-executes the composables that may have changed in response to state changes, and then updates the Composition to reflect any changes. A Composition can only be produced by an initial composition and updated by recomposition.
Creating your first Jetpack Compose project is surprisingly easy.
As Jetpack compose bottom sheets are not backed by navigation library, system back press closes the current screen instead of the bottom sheet. To handle these real-time use-cases, compose provides BackHandler function. First, let’s take a look at the function signature:
So it’s time to learn how to handle system back press in composable functions. Out-of-the-box system back press in compose operates based on the nav controller. So when user triggers back press event, by default compose internally navigates to the previous composable function in the navigation view.
There is also a dedicated navigation compose dependency that supports UI declared in Jetpack Compose toolkit. We just need to create the NavHost, pass the NavController instance and define composable destinations: It's very intuitive and straightforward. There is no unnecessary boilerplate code if we want to implement a simple navigation.
Out-of-the-box system back press in compose operates based on the nav controller. So when user triggers back press event, by default compose internally navigates to the previous composable function in the navigation view. To learn more about navigation in compose please read this article.
You can use BackHandler
:
@Composable
fun TestScreen() {
BackHandler {
// your action
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With