Private View rootView; // cache Fragment view @ Overridepublic View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {if (rootView = null) {rootView = inflater. inflate (R. layout. tab_fragment, null);} // The cached rootView needs to determine whether it has been added to the parent. If a parent needs to be deleted from the parent, or else the rootview has a parent error. ViewGroup parent = (ViewGroup) rootView. getParent (); if (parent! = Null) {parent. removeView (rootView);} return rootView ;}
The onCreateView method is called every time a Fragment is switched. As a result, the layout of each Fragment is re-painted and the original Fragment state cannot be maintained.
Solution: cache the View in the Fragment onCreateView method.
Avoid repeating the onCreateView method when multiple fragment switches under the same activity