Written in the beginning

The UI structure is not the Tablayout+viewPager+Fragment result, but the FragmentTabHost+Fragment structure, so there are some problems. I don’t know if you have come across it. Take a look at these issues from the source code level and write them out in the hope that you can judge good or bad.

Think about:

The company used to write the code in the onResume method, but later thought, this is obviously wrong. Everyone knows that the onResume method of the Fragment depends on the onResume method of the attached Activity. When you jump from the Fragment to another Activity and return, All fragments of the Activity attached to fragment use the onResume method. The onResume method in our project is some necessary network requests and some operations unrelated to logic, so I didn’t find any mistakes. I found this problem in the process of dynamic adaptation and studied it. Here’s how to do it!

A mining pit

SetUserVisibleHint ();

public void setUserVisibleHint(boolean isVisibleToUser) {
        if(! mUserVisibleHint && isVisibleToUser && mState < STARTED && mFragmentManager ! = null && isAdded()) { mFragmentManager.performPendingDeferredStart(this); } mUserVisibleHint = isVisibleToUser; mDeferStart = mState < STARTED && ! isVisibleToUser; }Copy the code

The Fragment+ViewPager framework allows you to use this method to determine whether your Fragment is visible or not. Viewpager and FragmentPagerAdapter with Viewpager

@Override
public Object instantiateItem(ViewGroup container, int position) {
	    ...
	    
        if(fragment ! = mCurrentPrimaryItem) { fragment.setMenuVisibility(false);
            fragment.setUserVisibleHint(false);
        }
        return fragment;
    }
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        Fragment fragment = (Fragment)object;
        if(fragment ! = mCurrentPrimaryItem) {if(mCurrentPrimaryItem ! = null) { mCurrentPrimaryItem.setMenuVisibility(false);
 
mCurrentPrimaryItem.setUserVisibleHint(false);
            }
            if(fragment ! = null) { fragment.setMenuVisibility(true);
                fragment.setUserVisibleHint(true); } mCurrentPrimaryItem = fragment; }}Copy the code

This method call is actually in the FragmentPagerAdapter. So when you are in the FragmentTabHost+Fragment structure, you will notice that this method will not be called at all.

Mining pit 2

OnHiddenChanged method. The source code comments are written very clearly.

/**
     * Return true if the fragment has been hidden.  By default fragments
     * are shown.  You can find out about changes to this state with
     * {@link #onHiddenChanged}. Note that the hidden state is orthogonal* to other states -- that is, to be visible to the user, A fragment * must be both started and not hidden. */ If the fragment object is already hidden, then it returnstrue. Fragments are displayed by default. You can use the onHiddenChanged(Boolean) callback to retrieve changes in the state of the Fragment object. Note that the hidden state is orthogonal to the other states - that is, to display the Fragment object to the user, the Fragment object must be started and not hidden.#### is worth our attentionHide or show means that the Fragment calls show or hider to change the value of mHiddenCopy the code

In the FragmentTabHost+Fragment structure, when you jump to another Activity and return, you’ll notice that the method doesn’t work because the current Fragment doesn’t change show or hide.

Mining pit 3:

IsVisible method

What if you use this method to determine if the current page is hidden? I tried is not good, first look at the source of this method:

	 final public boolean isVisible() {
        returnisAdded() && ! isHidden() && mView ! = null && mView.getWindowToken() ! = null && mView.getVisibility() == View.VISIBLE; }Copy the code

If you know the onHiddenChanged method, you should know that using it alone won’t work either, because the value of isHidden() is related to the onHiddenChanged method. When you switch TAB, isVisible() is put back to false.

conclusion

Take this opportunity to take a look at the detailed theory of these life cycles, so I solve the FragmentTabHost+Fragment structure as follows: can adapt to the first creation, TAB switch, jump Activity return various situations.

	    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(! hidden) { onResumeCommon(); }} //isVisible() focuses on the value of isHidden in the source code // If the Fragment object is already hidden, that is, after executing the hide () object, it returnstrue. @Override public voidonResume() {
        super.onResume();
        if (isVisible()) {
            onResumeCommon();
        }
    }

    private void onResumeCommon() {
            StatusBarUtil.setStatusBarColor(mActivity, R.color.white);
            StatusBarUtil.StatusBarLightMode(mActivity);
    }
Copy the code

Write in the last

Knowledge is not finished one day, continue to work hard!!