Hello, dear forum users. I use fragments and tabs. Fragments are switched using the viewPager and a fragment adapter for it. There was a problem with the fragment on which the camera is implemented: I have 5 fragments, a fragment with a camera in the middle (3), so if I switch to the second or fourth after it, then the surface view remains visible on them! (Without a camera, just a black place) But if I switch to the first and the last, then everything is perfectly hidden. Traced the lifecycle, when the first and the last one is called, he StopPaus, etc. with a camera with a ragenta, but with neighboring fragments not available. It is not difficult to understand that this is done for quick switching between the nearest fragments.

So how do I remove it? So that when switching neighbors too, onStop and it Pauses were also called

I tried to call them manually, it does not help ... I also read about the addToBactStack () method, which seems to help. But I have to call it when switching, and the adapter is doing this inside

Help please, thank you very much!

    2 answers 2

    ViewPager.setOffscreenPageLimit() . Setting the value to 0 will give you the desired result.

    • Alas, it does not always help in such cases, although with large volumes this should not be forgotten - gadfil

    Had a similar problem when each tab of the tab had its own viewPager. When working with viewPager, fragments are not destroyed, the onDestroy method is not called.
    I decided to be rather clumsy, but I had only two tabs and, accordingly, the memory on my crutch was spent is not critical. I used two viewPager and arranged them in one frame, simply changing their size.

    • Hmm, but the MB does not use the Pager view at all. MB to replace fragments myself ... try like me - justix
    • Accidentally found the element FragmentTabHost, he saved me) - justix