title: How to handle the code when onSurfaceChanged method is being called twice
body: <p>sometimes depending on device, onSurfaceChanged method called more than once, which results in crash of camera preview(I am using camera2).
  I have no idea how resolve this problem. Here is a part of my code:</p>
  
  <pre><code>@Override
      public synchronized void onSurfaceChanged(GL10 gl, int width, int height) {
      //  if(!(width == mWidth || height == mHeight)) {
              mWidth = width;
              mHeight= height;
              Log.e("ex1", "onSurfaceChanged = " + width + ", height = " + height);
  
              //generate camera texture------------------------
              setupSurfaceTexture(width, height);
              surfaceIsDestroyed = false;
          //}
  
      }
  
  private void setupSurfaceTexture(final int width,final int height) {
         System.out.println("setupSurfaceTexture is being called");
          mCameraTexture.init();
  
          //set up surface texture------------------
          SurfaceTexture oldSurfaceTexture = mSurfaceTexture;
          mSurfaceTexture = new SurfaceTexture(mCameraTexture.getTextureId());
          mSurfaceTexture.setOnFrameAvailableListener(this);
          if(oldSurfaceTexture != null) {
              oldSurfaceTexture.release();
          }
  
          //get the camera orientation and display dimension, open camera------------
          int orientation = mContext.getResources().getConfiguration().orientation;
  
          float rotation = 90; 
          if((orientation == 1) &amp;&amp; (newApi)) { // TODO: NEED TO BE CHANGED
              isPortrait = true;
              rotation = 0; 
          }
          icam.openCamera(width, height, mSurfaceTexture);
          System.out.println("Is new API: " + newApi);
  
          if(mContext.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
              Matrix.setRotateM(mOrientationM, 0, rotation, 0f, 0f, 1f);
              if(newApi) {
                  icam.updateScaling(width, height, isPortrait);
                  mRatio[0] = icam.getCameraWidthOrScaleX();
                  mRatio[1] = icam.getCameraHeightOrScaleY();
              } else {
                  icam.updateScaling(width, height, isPortrait);
                  mRatio[0] = icam.getCameraWidthOrScaleX();// * 1.0f/height;
                  mRatio[1] = icam.getCameraHeightOrScaleY();// * 1.0f/width;
              }
  
          } else {
              Matrix.setRotateM(mOrientationM, 0, rotation, 0f, 0f, 1f);
              if(newApi) {
                  mRatio[1] = icam.getCameraWidthOrScaleX();
                  mRatio[0] = icam.getCameraHeightOrScaleY();
              } else {
                  mRatio[1] = icam.getCameraWidthOrScaleX();//*1.0f/height;
                  mRatio[0] = icam.getCameraHeightOrScaleY();//*1.0f/width;
              }
          }
          Log.d("Rotation Portrait", mRatio[0] + "-----" + mRatio[1]);
  
          //start render-----
          requestRender();
          surfaceSetuped = true;
  }
  </code></pre>