Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override the system onbackpress() in Jetpack Compose

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()
 }
 ....
 ....
}
like image 997
Ranjithkumar Avatar asked Sep 12 '21 12:09

Ranjithkumar


People also ask

How do I make my screen scrollable in jetpack compose?

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 .

What is recomposition in jetpack compose?

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.

Is jetpack compose hard to learn?

Creating your first Jetpack Compose project is surprisingly easy.

How to back up jetpack compose bottom sheet?

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:

How to handle system back Press in composable functions?

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.

How to implement simple navigation using jetpack compose?

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.

What is out-of-the-box system back Press in compose?

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.


1 Answers

You can use BackHandler:

@Composable
fun TestScreen() {
    BackHandler {
        // your action
    }
}
like image 129
Philip Dukhov Avatar answered Oct 16 '22 22:10

Philip Dukhov