voyager icon indicating copy to clipboard operation
voyager copied to clipboard

Android Activity Lifecycle events stop being forwarded by AndroidScreenLifecycleOwner after screen rotation

Open beyondeye opened this issue 1 year ago • 1 comments

I am using the latest version of voyager 1.1.0-beta02

I need to listen to Activity ON_PAUSE and ON_RESUME events see following code

import android.util.Log
import androidx.compose.runtime.Composable
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import cafe.adriel.voyager.core.screen.Screen


class LifecycleEventsAfterScreenRotationBug :Screen{
    @Composable
    override fun Content() {
        LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
            Log.d("VOYAGER","ACTIVITY ON_RESUME")
        }

        LifecycleEventEffect(Lifecycle.Event.ON_PAUSE) {
            Log.d("VOYAGER","ACTIVITY ON_PAUSE")
        }
    }
}

voyager binds LocalLifecycleOwner to voyager custom AndroidScreenLifecycleOwner that forwards activity lifecycle events to user code. It works fine until the screen is rotated, and the activity recreated: from that point and on, unless we exit and reenter the screen, the activity lifecycle events are not forwarded anymore, i.e. in the code above, the two LifecycleEventEffect are not triggered any more

beyondeye avatar Jul 22 '24 07:07 beyondeye