3

I am creating an app using the Jetpack Compose. The app does some networking search in the background. I wanted to cancel the search if the user decided to press the back button. so I did it using DisposableEffect.

@Composable
fun SecondScreen() {
    val vm: MainViewModel = hiltViewModel()

    DisposableEffect(key1 = vm) {
        onDispose {
            vm.cancelSearch()
        }
    }
}

I know that onDispose will be called when the composable destroyed. But now the search getting canceled everytime when I rotate the screen. I can understand that because the composable destroyed and re-created.

I wonder if I could some how detect the screen rotation lifecyle, maybe I can prevent this behaviour. Or is there a better way to cancel the search when user press back?

Jeeva
  • 3,975
  • 3
  • 23
  • 47
  • You can always prevent your activity from being destroyed when configurations changes occur which includes device orientation changes. See https://stackoverflow.com/questions/4568558/how-to-avoid-restarting-activity-when-orientation-changes-on-android – Johann Feb 23 '22 at 15:03
  • I have some content that changes when the device on landscape mode. I want it to be re-composed when screen rotate. I am sure there has to be a better way to achieve this. As already said in my question, is there way to listen for the screen orientation change? – Jeeva Feb 23 '22 at 15:29
  • 1
    You can override the activity's back button and handle the back button event in a global event handler you set up for the back button. This handler can provide an observable or mutable state that your viewmodels can listen to and take the necessary action if the screen that the viewmodel is associated with is the current screen. I also developed Jetmagic - a framework that handles your use-case. Check it out here: https://github.com/JohannBlake/Jetmagic – Johann Feb 23 '22 at 15:43

2 Answers2

21

I wrote this fun and you can call this in every composable fun

@Composable
fun ComposableLifecycle(
    lifeCycleOwner: LifecycleOwner = LocalLifecycleOwner.current,
    onEvent: (LifecycleOwner, Lifecycle.Event) -> Unit
) {
    DisposableEffect(lifeCycleOwner) {
        val observer = LifecycleEventObserver { source, event ->
            onEvent(source, event)
        }
        lifeCycleOwner.lifecycle.addObserver(observer)
        onDispose {
            lifeCycleOwner.lifecycle.removeObserver(observer)
        }
    }
}

how to use:

@Composable
fun VideoPlayer() {
    ...
    ComposableLifecycle { source, event ->
        if (event == Lifecycle.Event.ON_PAUSE) {
            exoPlayer.pause()
        }
    }
}

Example in meduim

Mahdi Zareei
  • 1,299
  • 11
  • 18
1

Each reorganization of Jetpack Compose is a life cycle,Detect the horizontal and vertical state during each reorganization,please try

@Composable
fun Test() {
    var status by rememberSaveable{
        mutableStateOf(true)
    }
    LaunchedEffect(LocalConfiguration.current.orientation.absoluteValue){
        status = !status
    }
    Text(text = "is Horizontally $status")
}
Yshh
  • 654
  • 1
  • 10