Not sure if you still looking for a single-fragment solution. But I manage to switch the front/back camera using following code without creating another fragment. So, you may modifying a bit and it should fit yours.
My solution is as simple as removing the current CameraView instance and add a new one. If you prefer add it to another frame, be my guest.
The CameraView is added dynamically into a Framelayout:
public class MyCameraFragment extends CameraFragment {
CameraView cameraView;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
addCameraView(view);
}
private void addCameraView(View view) {
FrameLayout frame = (FrameLayout)v.findViewById(R.id.cameraFrame);
frame.removeAllViews();
cameraView = new CameraView(getActivity());
cameraView.setHost(cameraHost = new MyCameraHost(getActivity()));
setCameraView(cameraView);
frame.addView(cameraView);
}
private void doSwitchCamera() {
// do some change to the settings.
useFrontFacingCamera = !useFrontFacingCamera;
if (null != cameraView) {
cameraView.onPause();
}
addCameraView(getView());
cameraView.onResume();
}
}
I'm not sure if super.onPause() and super.onResume() is necessary but they do call the onPause() and onResume() of CameraView, which I believe it is necessary.
Edited: after some code investigation and testing, I found that calling super.onPause/onResume is not necessary. Just call onPause and onResume on the cameraView is enough.