How to override the system onbackpress() in Jetpack Compose

Viewed 7045

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()
 }
 ....
 ....
}
1 Answers

You can use BackHandler:

@Composable
fun TestScreen() {
    BackHandler {
        // your action
    }
}
Related