Skip to content

Conversation

@KirillCustom
Copy link
Contributor

Fix camera preview not displaying after view remount

Problem

When CameraView component is unmounted and remounted (e.g., navigating away and back to the screen), the camera preview stops displaying even though scanning continues to work.

Root cause: When the view is unmounted, onDetachedFromWindow() calls releaseCamera() which sets previewView = null. When the view is remounted, the new previewView instance is not automatically bound to the camera manager, so bindCameraUseCases() tries to set the surface provider on a null reference:

preview = Preview.Builder().build().also {
    previewView?.let { view ->  // previewView is null!
        it.setSurfaceProvider(view.surfaceProvider)
    }
}

This results in a camera that scans barcodes successfully but shows no preview image.

Solution

Added bindPreviewView() call in onAttachedToWindow() lifecycle method.

When the view is reattached to the window:

  1. setupLayoutHack() restarts the layout callback
  2. bindPreviewView(previewView) re-binds the new preview view instance to the camera manager
  3. If scanning is active, bindCameraUseCases() is called automatically with the correct previewView reference

Changes

 override fun onAttachedToWindow() {
     super.onAttachedToWindow()
     // Restart layout callback when view is reattached
     setupLayoutHack()
+    // Rebind the preview view to camera manager when reattached
+    cameraManager?.bindPreviewView(previewView)
 }

Testing

Tested on Android with the following scenario:

  1. Navigate to screen with CameraView
  2. Camera preview displays correctly ✅
  3. Navigate away from screen
  4. Navigate back to screen
  5. Camera preview displays correctly ✅ (previously showed black screen)
  6. Barcode scanning works correctly ✅

Benefits

  • Camera preview now works correctly after view remount
  • Minimal code change - only one line added
  • No changes to cleanup logic - releaseCamera() still works as intended
  • No memory leaks
  • Maintains backward compatibility

When CameraView is unmounted and remounted (e.g., navigating away and back), the preview stopped displaying. This happened because onDetachedFromWindow() released the camera and set previewView to null, but the new previewView instance wasn't rebound when the view was reattached.
Added bindPreviewView() call in onAttachedToWindow() to rebind the preview view to the camera manager when the view is reattached to the window.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where the camera preview stops displaying after the CameraView component is unmounted and remounted, while barcode scanning continues to function. The fix ensures that when the view is reattached to the window, the preview view is properly rebound to the camera manager.

Key Changes:

  • Added bindPreviewView() call in the onAttachedToWindow() lifecycle method to rebind the preview view after remount

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Owner

@pushpender-singh-ap pushpender-singh-ap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pushpender-singh-ap pushpender-singh-ap changed the title Fix camera preview not displaying after view remount fix: camera preview not displaying after view remount Oct 27, 2025
@pushpender-singh-ap pushpender-singh-ap merged commit e8563e5 into pushpender-singh-ap:main Oct 27, 2025
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants