Add ability to clear lifecycleObserver (#798)

* Added ability clear lifecycleObserver

* Added clearing reference on lifecycle
pull/789/merge
Alexander 5 years ago committed by GitHub
parent 778cdedc00
commit 5530c011fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -752,13 +752,27 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
* Sets the lifecycle owner for this view. This means you don't need
* to call {@link #open()}, {@link #close()} or {@link #destroy()} at all.
*
* If you want that lifecycle stopped controlling the state of the camera,
* pass null in this method.
*
* @param owner the owner activity or fragment
*/
public void setLifecycleOwner(@NonNull LifecycleOwner owner) {
if (mLifecycle != null) mLifecycle.removeObserver(this);
public void setLifecycleOwner(@Nullable LifecycleOwner owner) {
if (owner == null) {
clearLifecycleObserver();
} else {
clearLifecycleObserver();
mLifecycle = owner.getLifecycle();
mLifecycle.addObserver(this);
}
}
private void clearLifecycleObserver() {
if (mLifecycle != null) {
mLifecycle.removeObserver(this);
mLifecycle = null;
}
}
/**
* Starts the camera preview, if not started already.

Loading…
Cancel
Save