title: How to make Barcode Scanner only scan once (Google Mobile Vision API)?
body: <p>I'm developing an app that has Barcode Scanner using google mobile vision api. I should be able to scan in ScannerClassy.java only once and save the result in MainActivity. The problem is when I scanned the barcode, it opened MainActivity.java 5-7 times. I tried to use camerasource.release() but it did not work.</p>
  <h3>ScannerClass.java</h3>
  <pre><code>    import com.example.MashapeHello.R;
      import com.google.android.gms.vision.Detector;
      import android.Manifest;
      import android.app.Activity;
      import android.content.Intent;
      import android.content.pm.PackageManager;
      import android.os.Looper;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.support.v4.app.ActivityCompat;
      import android.util.Log;
      import android.util.SparseArray;
      import android.view.SurfaceHolder;
      import android.view.SurfaceView;
      import android.widget.TextView;
  
      import com.google.android.gms.vision.CameraSource;
      import com.google.android.gms.vision.barcode.Barcode;
      import com.google.android.gms.vision.barcode.BarcodeDetector;
      import java.io.IOException;
  
  
      public class ScannerClass extends AppCompatActivity implements Detector.Processor {
          private TextView textView;
          private SurfaceView surfaceView;
          private BarcodeDetector barcodeDetector;
          private CameraSource cameraSource;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
  
          barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.ALL_FORMATS).build();
          barcodeDetector.setProcessor(this);
  
          cameraSource = new CameraSource.Builder(getApplicationContext(), barcodeDetector).
                  setRequestedPreviewSize(1024,1024).setAutoFocusEnabled(true)
                  .build();
  
          final Activity activity = this;
  
          surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
              @Override
              public void surfaceCreated(SurfaceHolder holder) {
                  try{
                      if(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
                          ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA},1024);
                          return;
                      }
                      cameraSource.start(surfaceView.getHolder());
                  } catch (IOException ie){
                      Log.e(&quot;Camera start problem&quot;, ie.getMessage());
                  }
              }
  
              @Override
              public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  
              }
  
              @Override
              public void surfaceDestroyed(SurfaceHolder holder) {
                  cameraSource.stop();
              }
          });
          }
  
  
          @Override
          public void release(){
  
          }
  
  
          @Override
          public void receiveDetections(Detector.Detections detections) {
          final SparseArray&lt;Barcode&gt; barcodes = detections.getDetectedItems();
          if (barcodes.size() != 0) {
              final StringBuilder sb = new StringBuilder();
              for (int i = 0; i &lt; barcodes.size(); ++i) {
                  sb.append(barcodes.valueAt(i).rawValue).append(&quot;\n&quot;);
              }
              Intent intent = new Intent(this, MainActivity.class);
              intent.putExtra(&quot;barcodevalue&quot;, sb.toString());
              startActivity(intent);
              cameraSource.release();
  
  
          }
      }
  }
  </code></pre>