I am using a viewpager with tab navigation for an Android project. One of my tabs has a surfaceview with a camera preview. The problem is that whenever I navigate to a neighboring tab, the preview in the camera tab starts even though the camera tab is not visible. Not only does this slow my app down, but if I then navigate to another tab and then to the camera tab, my app attempts to restart the preview thus resulting in a crash.
I'm looking for advice on how to only start a camera preview if the fragment is visible(ie. tab is active). And to finish preview as soon as tab is switched.
Thank you.