Lifecycle binding
See original GitHub issuein CameraFragment you call
CameraX.bindToLifecycle(this, preview, imageCapture, imageAnalyzer)
meaning you bind it to fragment’s lifecycle. But you call this method on onViewCreated()
which means you would receive the old lifecycleOwner. To avoid crashing you every time also call CameraX.unbindAll()
which is some sort of work around.
Instead it would be better to call CameraX.bindToLifecycle()
with viewLifecycleOwner. Then you can delete CameraX.unbindAll()
Thus, it seems like you have misused fragment’s lifecycle with view’s lifecycle. Correct me I am wrong. Thanks
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Bind layout views to Architecture Components
The layouts in your app can bind to the data in the Architecture Components, which already help you manage the UI controllers lifecycle...
Read more >Handling Lifecycle with View Binding in Fragments - Medium
View Binding is an upcoming feature in Android, available in Android Studio 3.6 Canary 11+ which allows you to more easily interact with...
Read more >Binding lifecycle events - Knockout.js
Binding lifecycle events · childrenComplete — This event is notified synchronously once the child nodes (and all synchronously loaded descendants) have been ...
Read more >Introduction to the Build Lifecycle - Apache Maven
Each packaging contains a list of goals to bind to a particular phase. For example, the jar packaging will bind the following goals...
Read more >Lifecycle of Binding Templates - Documentation - SAPUI5 SDK
The lifecycle of the binding templates differs from the lifecycle of controls that are contained in an aggregation. Whenever a control object is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Could you please explicitly specify where in the sample
CameraX *initialize*
happens?I’m not sure what you mean by “the old lifecycleOwner”. The current sample is using the fragment’s lifecycle, which usually is the same as the view’s lifecycle except
CameraFragment
sets the retainInstance flag.By attaching CameraX to the fragment lifecycle, we avoid CameraX being fully torn down (and thus closing the camera) upon config changes like rotation. The intent is to allow for CameraX to maintain some of its state by doing:
CameraX.unbindAll()
→CameraX.bindToLifecycle()
, which happens duringonViewCreated
.By using the view’s lifecycle, the order of events is:
CameraX *teardown*
→CameraX *initialize*
→CameraX.bindToLifecycle()
. However, I just tested this and it seems that this results in snappier transitions during device rotations on a Pixel 3 device. I’m guessing it’s due to the teardown starting as the view gets destroyed, rather than doing everything duringonViewCreated
.I will investigate the potential implications of switching to the view’s lifecycle. It’s likely that devices with slow camera open/close will have degraded performance using this path. We have to take into consideration other things such as navigation, multi-window environments, etc.