From 5530c011fa5258c3533bd87a98c2a2bc344978f1 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 1 Apr 2020 22:18:52 +0300 Subject: [PATCH] Add ability to clear lifecycleObserver (#798) * Added ability clear lifecycleObserver * Added clearing reference on lifecycle --- .../otaliastudios/cameraview/CameraView.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 07c1d6b4..5933df05 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -752,12 +752,26 @@ 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); - mLifecycle = owner.getLifecycle(); - mLifecycle.addObserver(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; + } } /**