r/android_devs • • 1d ago

Article Why our Android camera stream stopped rotating when we detached the preview

I am working on Guardian Eye WebCamera. In our Android app, we can display a clock in place of the camera viewfinder while the camera continues to stream video. Previously, turning the phone with the clock on screen could leave the stream in its old orientation. The same rotation worked correctly with the viewfinder visible.

Video capture is handled by a service, and the Activity provides a preview surface whenever the image needs to be displayed. The service’s rotation handler used to check whether this surface existed before applying a rotation in Auto mode. We added that check to preserve the capture orientation when the user went to the home screen or locked the phone. Replacing the viewfinder with the clock also removed the surface. MainActivity stayed in the Resumed state. The rotation handler returned early when it found the surface missing, so the stream kept its previous orientation.

We moved this check to the Activity lifecycle. In onResume(), the Activity publishes a new identifier to a StateFlow<Long?>. In onPause(), it clears the identifier if it still owns it. In Auto mode, the service applies display rotation changes while the Activity is resumed. When it receives a new identifier, it also reads the current display rotation. This lets it catch up on changes that occurred while the Activity was paused or being recreated. A separate collector handles the preview surface.

The identifier changes with every onResume() call. With a Boolean flag, a slow collector can miss the brief pause between two resumes. If it last saw true and the Activity has already resumed, it sees the same value and skips reading the current display rotation. A new identifier gives the collector a distinct value even if it misses the pause. The ownership check also lets an older Activity clear only its own identifier, so a late onPause() leaves the new Activity’s state intact.

The service can also restart while the clock is on screen. The repository keeps the current Activity identifier across a service reset, so the new service can read the current display rotation at startup without waiting for the user to reopen the viewfinder.

The repository tests cover detaching the preview while the Activity remains resumed, closing an old resume handle after a new one is registered, and preserving the current identifier across a service reset. The device check looks at decoded frame dimensions as the operator rotates the phone with the clock on screen. The operator then goes to the home screen, rotates the phone again, and returns to the app. The stream should keep its previous orientation on the home screen and pick up the current display rotation when the app resumes.

2 Upvotes

1 comment sorted by

1

u/AutoModerator 1d ago

Your post will be checked before being published since your account age is low.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.