Why is the onResume cycle method of the next page called when switching fragment?

problem description

if the onResume event of the second page is triggered when the first page is opened, the onResume of the third page will trigger the fragment. The life cycle of the fragment is onAttach ()-> onCreate ()-> onCreateView ()-> onActivityCreated ()-> onStart ()-> should not be triggered when the status of the future should be displayed?

the environmental background of the problems and what methods you have tried

I guess maybe the way viewpager displays fragment is to display three at the same time to prevent stutters when you open the next page.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
ViewPager viewPager = findViewById (R.id.viewPager);
viewPager.setCurrentItem (2);

what result do you expect? What is the error message actually seen?

is there a way to trigger the onResume event only when a page is opened?

Feb.17,2022

my guess is that when you open a page, it does realize all the lifecycle of the next page in advance. After searching, it is found that the real way to deal with the display of the page is setUserVisibleHint:

.
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        ....
    }

viewpager has a preloading mechanism, that is, the next page has actually been rendered


ViewPager has a preloading mechanism, and the number of preloaded pages on the left and right sides is always not less than 1

if you want to load when the user is visible, you can override the setUserVisibleHint (boolean) method, but it should be noted that the timing of calling this function will cause your App to crash, because when it is called, there is no guarantee that the views have been created. You can find out why by referring to the source code of FragmentPagerAdapter and FragmentStatePagerAdapter . Therefore, you can refer to the following code:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (getView() != null) {
        ... ...
    }
}

also note that setUserVisibleHint (boolean) in the first Fragment in ViewPager is earlier than onActivityCreated (Bundle) and onViewCreated (View, Bundle) , so the first (or directly newly created) Fragment is handled differently from the preloaded one. The optimization code is as follows:

/**  */
private boolean mCreatedAndVisible = false;

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    callWhenCreatedAndVisible();
}

public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        callWhenCreatedAndVisible();
    } else {
        mCreatedAndVisible = false;
    }
}

private void callWhenCreatedAndVisible(boolean useLooper) {
    if (!mCreatedAndVisible && getUserVisibleHint() && getView() != null) {
        mCreatedAndVisible = true;
        // TODO ...
    }
}

public void onDestroyView() {
    mCreatedAndVisible = false;
    super.onDestroyView();
}
Menu