Greetings.

I use the standard TabbedActivity in AndroidStudio , in which the MainActivity class MainActivity , the PlaceholderFragment fragment and its Adapter implemented in the MainActivity class, I registered the logs and when the program is onCreate method of the Main Activity and twice the onCreateView Fragment called in the logs, I cannot understand why onCreateView called twice? I have implemented a MediaPlayer in a Fragment , and if I scroll once, it does not go to the stop state the first time, although in the onStop method and onDestroyView I install MediaPlayer.stop . It is necessary to scroll twice, only then the onStop method onStop , why is that? How do I catch the second onCreateView ? I suppose one Fragment I see directly, and the other lives in memory? If so, how can I access it and set the necessary parameters in its onStop and onDestroyView ?

    1 answer 1

    You should read more about the PagerAdapter and FragmentPagerAdapter .

    the logs call the onCreate method of the main Activity and twice the onCreateView Fragment

    • This happens in order to ensure a smooth transition to the next page! After all, if you smoothly slide your finger in the direction of the next page - you already see the finished fragment.

    I suppose one Fragment I see directly, and the other lives in memory

    • When you go to the second fragment, 3 fragments already live in memory;) the one that is on the left and the one on the right.

    To solve your problem, you most likely should subscribe to the OnTabSelectedListener event, and via the adapter, issue commands to stop or start MediaPlayer.