Skip to content
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

Close camera on backgrounding the app #58

Closed
wants to merge 9 commits into from

Conversation

davidjiagoogle
Copy link
Collaborator

Before the change, PreviewViewModel.stopCamera() only cancels the job. If the camera is already open and the useCases already bound, it doesn't do anything, resulting in the camera still being open even when the app is backgrounded.

In this PR, cameraProvider.unbindAll() is called in stopCamera so that existing bound useCases would unbind and the camera would stop. Since this is evoked in lifeCycle's onStop, the crash is fixed

Copy link
Member

@yasith yasith left a comment

Choose a reason for hiding this comment

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

As an aside, I think some of your older branches are attached to your current pull requests for some reason.

If you pull on main, and branch from there I think it'll be fixed for future PRs.

Not a big problem, but when merging, you should make sure to remove the unnecessary details from the commit description.

@@ -100,6 +100,9 @@ class PreviewViewModel @Inject constructor(
cancel()
}
}
viewModelScope.launch {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should handle this from inside the CameraUseCase without needing the ViewModel's manual intervention.

When runningCameraJob.cancel() is called I think we'll get to awaitCancellation() in CameraXCameraUseCase.

From there we can do a finally block, and call cameraProvider.unbindAll()

Lets wait to hear from @temcguir on this too.

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