OnStart is called only once in the fragment

Asked 2 years ago, Updated 2 years ago, 65 views

The first three fragments I reduced it to two From then on, when the app turns on, it runs only once When there were three fragments, it was called well when moving the fragments Why is that?ㅜ<

onstart fragment

2022-09-22 15:13

1 Answers

The information you posted is insufficient, but if you are using a view page and the adapter has reduced the number of items from three to two, this phenomenon may occur. When the number of items is three (=three fragments), the fragments of the 0th index are deleted when you move from the 0th index to the 2nd index. When you move back from the second index to the first index, a fragment of the 0th index is created.

In other words, onStart() is called because the fragment generation and deletion are repeated when the viewpager is moved. Do not delete internally when there are two fragments. Therefore, onStart() is called only once. This is purely due to the internal behavior of the viewpager. Use the function below setOffscreenPageLimit() in the view page to adjust the number of pages to be maintained on the left and right (the default number is 1)

The conclusion is that if you are using a viewfager, you should not write code assuming that the fragment's onStart() occurs every time you move the page.

Refer to the illustration in the ViewPager – How Do You Work section below for hints on how the viewpager manages pages internally.


2022-09-22 15:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.