Fragment life cycle problems in different scenarios

How to get the Show/Hide page event of the Fragment page?

Due to the variety of fragment usage scenarios, it is inaccurate to rely on OnResume/OnPause callbacks to indicate fragment Show/Hide, for example:

Scenario 1: When
an Activity on the home page carries multiple Fragment Tabs, switching between tabs will not trigger OnResume/OnPause of Fragment. The triggered callback function is onHiddenChanged(boolean hidden)

Scenario 2:
When a ViewPager hosts Fragments of multiple pages


In Scenario 1: When the first Fragment1 is displayed, although the second Fragment2 has not been displayed yet, the OnResume of Fragment2 is still executed and is in the resumed state.

The callback of the state at this time is: onHiddenChanged() method
or use isHide() method to judge the current display state
instead of OnResume/OnPause


In scenario 2: ViewPager page switching OnResume/OnPause/onHiddenChanged is not triggered, and the triggered callback is setUserVisibleHint.

The callback of this state is: setUserVisibleHint() method
or use getUserVisibleHint() method to judge the current display state
instead of OnResume/OnPause

onResume()
onPause()
onHiddenChanged(boolean hidden)
setUserVisibleHint(boolean isVisibleToUser)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325750719&siteId=291194637