-
-
Notifications
You must be signed in to change notification settings - Fork 21
Fix Android background flickering during camera initialization #279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Use solid black background initially and transition to transparent only after camera is ready. This prevents flickering during camera initialization on Android. Co-authored-by: riderx <[email protected]>
Ensure window and webView backgrounds are restored if camera initialization fails, preventing black screen on error. Co-authored-by: riderx <[email protected]>
- Extract webView background restoration to helper method - Simplify null-safe chain for better readability - Add proper logging for error cases instead of silently ignoring exceptions Co-authored-by: riderx <[email protected]>
- Store original webView background color to restore properly - Extract isToBackMode() helper to reduce code duplication - Add comments explaining the approach Co-authored-by: riderx <[email protected]>
- Store sessionConfig.isToBack() result to avoid checking null twice - Capture originalWindowBackground in local variable before async call - Clear originalWindowBackground immediately to prevent double-restoration Co-authored-by: riderx <[email protected]>
- Capture sessionConfig in local variable before accessing its methods - Use synchronized block to prevent race condition with multiple error calls - Ensure only one thread restores the background Co-authored-by: riderx <[email protected]>
- Add null check for webView in restoration lambda - Capture background color before posting to UI thread - Improve comments explaining thread-safety and WebView background assumptions Co-authored-by: riderx <[email protected]>
|
@copilot lint is failing ensure all test pass |
- Remove trailing whitespace - Reformat chained method calls per prettier-plugin-java rules Co-authored-by: riderx <[email protected]>
Fixed Java code formatting issues in commit 2989899. The Java files now pass Note: There appears to be a pre-existing issue with the |
Fix Android Background Flickering ✅
Problem
When calling
CameraPreview.start()on Android, multiple UI rendering issues occurred:Root Cause
The flickering occurred because transparency was applied to the window background and webView immediately, before the camera preview was ready. This triggered layout invalidations and redraws before camera frames were available.
Solution
Changed the initialization sequence to:
Implementation Details
CameraPreview.java (43 lines added):
Color.BLACKfor window background during initialization (line 941)isToBackMode()helper method to reduce code duplication (lines 1497-1506)Color.TRANSPARENTinonCameraStarted()callback (lines 1639-1647)CameraXView.java (37 lines added):
Color.BLACKfor webView background during initialization (line 452)Color.TRANSPARENTafter camera binds successfully (lines 934-940)restoreWebViewBackground()helper with full thread-safety and null checks (lines 426-439)Code Quality
Next Steps
Files Changed
android/src/main/java/app/capgo/capacitor/camera/preview/CameraPreview.java(+43 lines)android/src/main/java/app/capgo/capacitor/camera/preview/CameraXView.java(+37 lines)Total: 2 files changed, 77 insertions(+), 3 deletions(-)
Original prompt
This section details on the original issue you should resolve
<issue_title>bug: Background flickering on Android.</issue_title>
<issue_description>## Bug Report
Capacitor Version
Plugin Version
context(s)
Platform(s)
Android
Current Behavior
When calling CameraPreview.start() on Android, several UI rendering issues occur:
it's not being clicked and the startPreview code is not running
banner begins flickering
background correctly turns black
Workaround: Putting the app into background and foreground multiple times
eventually resolves all the flickering issues.
Implementation details:
Expected Behavior
The camera preview should start smoothly without any UI flickering, similar to
the behavior on iOS. The background should immediately be transparent/black,
buttons should not show false clicking animations, and banners should remain stable.
Code Reproduction
GitHub Repo
The source code is generated by OutSystems ODC and can be built directly in Android Studio. I did have to run npm ci prior to building it as that will update capacitor to the correct version. OutSystems does this in the build process in the cloud.
I am happy to get you any other files you are in need of. I am not completely clear on what OutSystems gives when I download the source code but I can send you snippets of the javascript if need be.
Other Technical Details
npm --versionoutput: 10.9.4node --versionoutput: v22.21.1Build System: OutSystems ODC Cloud Build Pipeline
Android Galaxy A16 5G
One UI version 8.0
Android version 16
Additional Context
I have messed around with setting html, body, .conten...
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.