title: android camera2 preview surface in case of use background service
body: <p><strong>tl;dr</strong> I have activity and background service with camera2api - don't know how to pass "stream" preview to activity view.</p>
  
  <p>I know how to make camera preview in Activity. I create e.g. SurfaceView in activity layout, and pass its surface (by this way:)</p>
  
  <pre><code>Surface surface = surfaceViewFromLayout.getHolder().getSurface();
  myCameraObjWhichHandlesCamera2API.open(surface);
  </code></pre>
  
  <p>(ofcourse also with code to set appropriate size)</p>
  
  <p>as a preview target to</p>
  
  <pre><code>CameraDevice.createCaptureSession(List&lt;Surface&gt; outputs, CameraCaptureSession.StateCallback callback, Handler handler)
  </code></pre>
  
  <p><strong>Question</strong>: But how to do to have working capture session in background service (still doable for me) and showing preview in Activity when screen is on? More context: I am writing app to make timelapses - shoot photo every e.g. 30 seconds. Camera code is in background service. I want to sometimes turn on the screen and look for progress and also see current view from camera.</p>
  
  <p>The major thing is that I have two components -  Activity and Background Service with camera, and don't know how to pass current camera preview "stream".</p>
  
  <ol>
  <li>If I could pass Surface object from Activity layout to background service component, what if Activity gets destroy by turn off screen - Surface may also get destroyed, hence capture session also.</li>
  </ol>
  
  <p><strong>edit:</strong> But from what I know, it is not possible to pass objects between components - except parcelable/serializable, but Surface is rather more complicated object?</p>
  
  <ol start="2">
  <li>Don't pass layout's surface to background service camera, but create dummy surface (<em>code below</em>) in background service - but how to pass that "stream" between components, set SurfaceView in Activity to that.</li>
  </ol>
  
  <p><code>SurfaceTexture dummySurface = new SurfaceTexture(10);
  dummySurface.setDefaultBufferSize(choosenSize.getWidth(), choosenSize.getHeight());
  Surface previewSurface = new Surface(dummySurface);</code></p>
  
  <p><code>// then when creating request, pass this:
  cameraDevice.createCaptureSession(Arrays.asList(previewSurface, imageReader.getSurface(), stateCallback, null);</code></p>