title: Android Camera preview bug
body: <p>Here's my CameraView class:</p>
  
  <pre><code>public class CameraView extends SurfaceView implements SurfaceHolder.Callback, PreviewCallback {
  private Camera camera=null;
  
  public CameraView(Context context, AttributeSet attrs) {
      super(context, attrs);
  
      SurfaceHolder holder = this.getHolder();
      holder.addCallback(this);
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }
  
  public boolean isVisible(){
      return this.getVisibility()==View.VISIBLE;
  }
  
  public void show(){
      this.setVisibility(View.VISIBLE);
  }
  
  public void hide(){
      this.setVisibility(View.GONE);
  }
  
  public void surfaceCreated(SurfaceHolder holder) {
      camera = Camera.open();
      camera.setPreviewCallback(this);
  
      try {
          camera.setPreviewDisplay(holder);
      }catch (IOException e) {
          Log.e("Preview Exception", e.getMessage());
      }
  }
  
  public void surfaceDestroyed(SurfaceHolder holder) {        
      if(camera!=null){
          camera.stopPreview();
          camera.setPreviewCallback(null);
          camera.release();
          camera=null;
      }
  }
  
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {        
      if(camera==null || !(w&gt;0&amp;&amp;h&gt;0))
          return;
  
      Camera.Parameters parameters = camera.getParameters();
      parameters.setPreviewSize(w, h);
  
      camera.setParameters(parameters);
      camera.startPreview();
  }
  
  @Override
  public void onPreviewFrame(byte[] data, Camera camera) {
  
  }
  }
  </code></pre>
  
  <p>In my Activity I have two buttons - one of them shows the camera preview, and second hides it (using CameraView.show() and CameraView.hide()) methods.</p>
  
  <p>It works almost fine, except one case:
  If I hide preview using CameraView.hide(), press the HOME button (app will be forked to background) and bring app to front again, I can't start the preview anymore.
  When I call CameraView.show() (by pressing a button), CameraView appears, but preview doesn't start.</p>
  
  <p>For me it looks like android bug, because in other cases everything works fine - I'm running android 2.1.</p>
  
  <p>Can someone help me?
  Thanks!</p>
  
  <p>PS. Here's the updated class, according to mmeyer's post (still not working):</p>
  
  <pre><code>public class CameraView extends SurfaceView implements SurfaceHolder.Callback, PreviewCallback {
  private Camera camera=null;
  private SurfaceHolder holder;
  
  public CameraView(Context context, AttributeSet attrs) {
      super(context, attrs);
  }
  
  public void initialize(){
      camera=Camera.open();
  
      holder = this.getHolder();
      holder.addCallback(this);
      holder.setSizeFromLayout();
      holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }
  
  public void release(){
      holder.removeCallback(this);
  
      if(camera!=null){
          camera.stopPreview();
          camera.setPreviewCallback(null);
          camera.release();
          camera=null;
      }
  }
  
  public boolean isVisible(){
      return this.getVisibility()==View.VISIBLE;
  }
  
  public void show(){
      this.setVisibility(View.VISIBLE);
  }
  
  public void hide(){
      this.setVisibility(View.GONE);
  }
  
  public void surfaceCreated(SurfaceHolder holder) {      
  }
  
  public void surfaceDestroyed(SurfaceHolder holder) {
      if(camera!=null)
          camera.stopPreview();
  }
  
  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {        
      if(camera==null || !(w&gt;0&amp;&amp;h&gt;0))
          return;
  
  
      try {
          camera.setPreviewDisplay(holder);
      }catch (IOException e) {
          Log.e("Preview Exception", e.getMessage());
      }
  
      Camera.Parameters parameters = camera.getParameters();
      parameters.setPreviewSize(w, h);
  
      camera.setParameters(parameters);
      camera.startPreview();
  }
  
  @Override
  public void onPreviewFrame(byte[] data, Camera camera) {
  
  }
  }
  </code></pre>