android - When using Voyager, Configuration Changes create problems with lifecycle handling. Demo Project included - Stack Overf

This is a Demo Project to illustrate a basic problem with Voyager navigation, lifecycle handling and co

This is a Demo Project to illustrate a basic problem with Voyager navigation, lifecycle handling and configuration changes.

Using Voyager (HomeScreen wrapped by Navigator) leads to problems with lifecycle handling and configuration changes.

Particularly, if a configuration change happens like screen rotation, the HomeScreen observer becomes unable to observe lifecycle events like the app getting in the background (ON_PAUSE).

This does not happen if Voyager is not used (for example, use GreetingView and see the logs).

Testing process:

--- Voyager usage with Home Screen ---

1.  Run the app
2.  Open Logs
3.  Put the app in the background and back to the foreground
4.  See the logs. Both HomeScreen and MainActivity onPause events are intercepted correctly.
5.  Now do some configuration changes like screen rotation
6.  Notice that HomeScreen cannot intercept ON_PAUSE events anymore.
7.  Put the app in the background and back to the foreground
8.  See the logs. HomeScreen does not intercept ON_STOP, ON_PAUSE, or ON_RESUME events.

--- App without Voyager | Use GreetingView ---

1.  Run the app
2.  Open Logs
3.  Put the app in the background and back to the foreground
4.  See the logs. Both HomeScreen and MainActivity onPause events are intercepted correctly.
5.  Now do some configuration changes like screen rotation
6.  All events are intercepted correctly.
7.  Put the app in the background and back to the foreground
8.  See the logs. HomeScreen intercepts ON_STOP, ON_PAUSE, and ON_RESUME events correctly.

The way lifecycleOwner is used in the project like that:

val lifecycleOwner = androidx.lifecyclepose.LocalLifecycleOwner.current

DisposableEffect(lifecycleOwner) {
    val observer = LifecycleEventObserver { _, event ->
        when (event) {
            Lifecycle.Event.ON_PAUSE -> {
                Log.d("GreetingView", "Lifecycle.Event.ON_PAUSE")
            }
            Lifecycle.Event.ON_RESUME -> {
                Log.d("GreetingView", "Lifecycle.Event.ON_RESUME")
            }
            else -> {
                Log.d("GreetingView", "Lifecycle.Event: $event")
            }
        }
    }
    lifecycleOwner.lifecycle.addObserver(observer)

    onDispose {
        lifecycleOwner.lifecycle.removeObserver(observer)
        Log.d("GreetingView", "Observer removed")
    }
}

If anyone could help identify the issue or solve the problem, it would be much appreciated.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744202065a4562926.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信