从源码的角度理解Fragment setUserVisibleHint与onHiddenChanged回调方法执行时机

Fragment一直是android开发者经常使用的控件,但是本人一直对这两个方法回调的时机弄得不是很清楚,今天就通过源码来看看,这两个api究竟怎么用。

setUserVisibleHint
    /**
     * Set a hint to the system about whether this fragment's UI is currently visible
     * to the user. This hint defaults to true and is persistent across fragment instance
     * state save and restore.
     *
     * <p>An app may set this to false to indicate that the fragment's UI is
     * scrolled out of visibility or is otherwise not directly visible to the user.
     * This may be used by the system to prioritize operations such as fragment lifecycle updates
     * or loader ordering behavior.</p>
     *
     * <p><strong>Note:</strong> This method may be called outside of the fragment lifecycle.
     * and thus has no ordering guarantees with regard to fragment lifecycle method calls.</p>
     *
     * @param isVisibleToUser true if this fragment's UI is currently visible to the user (default),
     *                        false if it is not.
     */
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (!mUserVisibleHint && isVisibleToUser && mState < STARTED
                && mFragmentManager != null && isAdded()) {
            mFragmentManager.performPendingDeferredStart(this);
        }
        mUserVisibleHint = isVisibleToUser;
        mDeferStart = mState < STARTED && !isVisibleToUser;
    }
* <p><strong>Note:</strong> This method may be called outside of the fragment lifecycle.
     * and thus has no ordering guarantees with regard to fragment lifecycle method calls.</p>

给系统设置一个提示当前fragment是否对用户可见,默认是可见的并且在fragment发生重启之后也会保存这个状态。注意看note,这个方法可能被调用在Fragment生命周期之外,并且不能保证这个方法与Fragment生命周期的排序。

查看源码,setUserVisibleHint被引用的状况,


image.png

发现在FragmentPageAdapter和FragmentStatePageAdapter中被调用了,看到这两个类是不是很熟悉,不就是我们常用的ViewPager+Fragment框架使用的适配器吗,下面我通过一个ViewPager+Fragment来查看
setUserVisibleHint的调用情况:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.ziv.myapplication.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/btn_next"
        android:orientation="horizontal" />
</RelativeLayout>
package com.example.ziv.myapplication;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "BaseFragment";
    private ViewPager mViewPager;
    private EditText etPageNum;
    private Fragment[] mFragments;
    private FragmentManager mFragmentManager;
    private Frgament1 mFrgament1;
    private Frgament2 mFrgament2;
    private Frgament3 mFrgament3;
    private Frgament4 mFrgament4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        setContentView(R.layout.activity_main);
        initViews();

        findViewById(R.id.btn_inflate_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = Integer.valueOf(etPageNum.getText().toString());
                Fragment fragment = mFragmentManager.findFragmentByTag(String.format("%s%d", "Frgament", num));
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                List<Fragment> fragmentList = mFragmentManager.getFragments();
                if (fragment == null) {
                    fragmentTransaction.add(R.id.layout_content, getFragmentByNum(num), String.format("%s%d", "Frgament", num));
                } else {
                    if (fragment.isAdded()) {
                        fragmentTransaction.show(fragment);
                    }
                }

                for (Fragment f : fragmentList) {
                    if (!f.equals(fragment)) {
                        fragmentTransaction.hide(f);
                    }
                }
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
    }

    private BaseFragment getFragmentByNum(int num) {

        BaseFragment fragment = null;

        switch (num) {
            case 1:
                fragment = mFrgament1;
                break;
            case 2:
                fragment = mFrgament2;
                break;
            case 3:
                fragment = mFrgament3;
                break;
            case 4:
                fragment = mFrgament4;
                break;
            default:
                fragment = mFrgament1;
                break;
        }
        return fragment;
    }

    private void initViews() {

        mViewPager = findViewById(R.id.layout_content);
//        etPageNum = findViewById(R.id.et_page_num);

        mFrgament1 = new Frgament1();
        mFrgament2 = new Frgament2();
        mFrgament3 = new Frgament3();
        mFrgament4 = new Frgament4();

        mFragments = new Fragment[]{mFrgament1, mFrgament2, mFrgament3, mFrgament4};
        mFragmentManager = getSupportFragmentManager();

        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setAdapter(new MyAdapter(mFragmentManager));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }

    private class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments[position];
        }

        @Override
        public int getCount() {
            return mFragments.length;
        }
    }
}
package com.example.ziv.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ziv on 2017/12/20.
 */

public class Frgament1 extends BaseFragment {

    private final static String TAG = "BaseFragment";

    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        Log.e("", "onCreateView rootView is null ?"+(rootView==null));
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_1, container, false);
        }
        return rootView;
    }
}

其他fragment类似,再来看看BaseFragment

package com.example.ziv.myapplication;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ziv on 2017/12/20.
 */

public class BaseFragment extends Fragment {

    private final static String TAG = BaseFragment.class.getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, this.getClass().getSimpleName() + "onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.e(TAG, this.getClass().getSimpleName() + "onAttach");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.e(TAG, this.getClass().getSimpleName() + "onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e(TAG, this.getClass().getSimpleName() + "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, this.getClass().getSimpleName() + "onResume");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, this.getClass().getSimpleName() + "onStop");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(TAG, this.getClass().getSimpleName() + "onPause");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, this.getClass().getSimpleName() + "onDestroy");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, this.getClass().getSimpleName() + "onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.e(TAG, this.getClass().getSimpleName() + "onDetach");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.e(TAG, this.getClass().getSimpleName()+"setUserVisibleHint isVisibleToUser is: " + isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.e(TAG, this.getClass().getSimpleName()+"onHiddenChanged hidden is: " + hidden);
    }
}

先来看看mViewPager.setOffscreenPageLimit(1);设置ViewPager预加载页数。看看看log:
首次加载时:


image.png

可以看到setUserVisibleHint是在onAttach之前调用的,因为设置了预加载,所以Fragment2的setUserVisibleHint也会被调用,然后就是正常的Fragment生命周期了。
滑动到Fragment2时:


image.png

由于预加载了一页,也就是当前Fragment的前后各自一页,所以这个时后Fragment3也会被加载,Fragment1这是后被缓存了。
滑动到Fragment3时:
image.png

由于只能预加载一页所以Fragment1被销毁了,setUserVisibleHint是在被加载或缓存的Fragment调用(Frgamen2/Fragment4),Fragment1已经被销毁了,如果是初次加载则在onAttach之前调用。
从Fragment3滑动到Fragment2:


image.png

综上所诉,setUserVisibleHint的调用与ViewPager预加载有关系,如果当前Fragment是属于预加载的页,setUserVisibleHint才会被调用。并且在onAttach之前调用,所以如果想要在setUserVisibleHint做懒加载,一定要判断当前Fragment的View是否已经初始化了。因为不去额定setUserVisibleHint的调用时机,所以做懒加载时最好在onResume和setUserVisibleHint都进行判断是否显示和是否Resumed的,示例代码如下:

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mVisibleToUser = isVisibleToUser;
        Log.debug(TAG, "setUserVisibleHint isVisibleToUser=" + isVisibleToUser + ", [" + getClass().getSimpleName() + "]");
        if(isVisibleToUser && mResumed) {
            Log.debug(TAG, "onPageStart !! [" + getClass().getSimpleName() + "]");
            onPageStart();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.debug(TAG, "onResume !! mVisibleToUser =" + mVisibleToUser + ", [" + getClass().getSimpleName() + "]");
        if(mVisibleToUser) {
            Log.debug(TAG, "onPageStart !! [" + getClass().getSimpleName() + "]");
            onPageStart();
        }
    }
onHiddenChanged
    /**
     * Called when the hidden state (as returned by {@link #isHidden()} of
     * the fragment has changed.  Fragments start out not hidden; this will
     * be called whenever the fragment changes state from that.
     * @param hidden True if the fragment is now hidden, false otherwise.
     */
    public void onHiddenChanged(boolean hidden) {
    }

大致意思就是Fragment的隐藏状态改变。隐藏状态,首先Fragment得隐藏。加载Fragment有两种方式,一种是replace,一种是add+show+hide;
这里先说结论:第一次add时是不会调用onHiddenChanged的,只有当一个Fragment之前的状态是hidden,然后show,这时候才会调用onHiddenChanged,或者之前的状态是show,然后置为hidden时,也会回调onHiddenChanged。现在从源码的角度来看,onHiddenChanged的调用时机:

    int num = Integer.valueOf(etPageNum.getText().toString());
                Fragment fragment = mFragmentManager.findFragmentByTag(String.format("%s%d", "Frgament", num));
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                List<Fragment> fragmentList = mFragmentManager.getFragments();
                if (fragment == null) {
                    fragmentTransaction.add(R.id.layout_content, getFragmentByNum(num), String.format("%s%d", "Frgament", num));
                } else {
                    if (fragment.isAdded()) {
                        fragmentTransaction.show(fragment);
                    }
                }

                for (Fragment f : fragmentList) {
                    if (!f.equals(fragment)) {
                        fragmentTransaction.hide(f);
                    }
                }
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

来看fragmentTransaction.show

    @Override
    public FragmentTransaction show(Fragment fragment) {
        addOp(new Op(OP_SHOW, fragment));

        return this;
    }
    void addOp(Op op) {
        mOps.add(op);
        op.enterAnim = mEnterAnim;
        op.exitAnim = mExitAnim;
        op.popEnterAnim = mPopEnterAnim;
        op.popExitAnim = mPopExitAnim;
    }
    static final class Op {
        int cmd;
        Fragment fragment;
        int enterAnim;
        int exitAnim;
        int popEnterAnim;
        int popExitAnim;

        Op() {
        }

        Op(int cmd, Fragment fragment) {
            this.cmd = cmd;
            this.fragment = fragment;
        }
    }
// cmd指的是当前Fragment的要进行的动作
    static final int OP_NULL = 0;
    static final int OP_ADD = 1;
    static final int OP_REPLACE = 2;
    static final int OP_REMOVE = 3;
    static final int OP_HIDE = 4;
    static final int OP_SHOW = 5;
    static final int OP_DETACH = 6;
    static final int OP_ATTACH = 7;
    static final int OP_SET_PRIMARY_NAV = 8;
    static final int OP_UNSET_PRIMARY_NAV = 9;

把当前操作存在一个List里,再来看看commit方法:

    @Override
    public int commit() {
        return commitInternal(false);
    }
    int commitInternal(boolean allowStateLoss) {
        if (mCommitted) throw new IllegalStateException("commit already called");
        if (FragmentManagerImpl.DEBUG) {
            Log.v(TAG, "Commit: " + this);
            LogWriter logw = new LogWriter(TAG);
            PrintWriter pw = new PrintWriter(logw);
            dump("  ", null, pw, null);
            pw.close();
        }
        mCommitted = true;
        if (mAddToBackStack) {
            mIndex = mManager.allocBackStackIndex(this);
        } else {
            mIndex = -1;
        }
        mManager.enqueueAction(this, allowStateLoss);
        return mIndex;
    }

接着来看enqueueAction,

    public void enqueueAction(OpGenerator action, boolean allowStateLoss) {
        if (!allowStateLoss) {
            checkStateLoss();
        }
        synchronized (this) {
            if (mDestroyed || mHost == null) {
                if (allowStateLoss) {
                    // This FragmentManager isn't attached, so drop the entire transaction.
                    return;
                }
                throw new IllegalStateException("Activity has been destroyed");
            }
            if (mPendingActions == null) {
                mPendingActions = new ArrayList<>();
            }
            mPendingActions.add(action);
            scheduleCommit();
        }
    }

再来看 scheduleCommit(),

    /**
     * Schedules the execution when one hasn't been scheduled already. This should happen
     * the first time {@link #enqueueAction(OpGenerator, boolean)} is called or when
     * a postponed transaction has been started with
     * {@link Fragment#startPostponedEnterTransition()}
     */
    private void scheduleCommit() {
        synchronized (this) {
            boolean postponeReady =
                    mPostponedTransactions != null && !mPostponedTransactions.isEmpty();
            boolean pendingReady = mPendingActions != null && mPendingActions.size() == 1;
            if (postponeReady || pendingReady) {
                mHost.getHandler().removeCallbacks(mExecCommit);
                mHost.getHandler().post(mExecCommit);
            }
        }
    }

这里是执行一个排队的动作

    Runnable mExecCommit = new Runnable() {
        @Override
        public void run() {
            execPendingActions();
        }
    };
    /**
     * Only call from main thread!
     */
    public boolean execPendingActions() {
        ensureExecReady(true);

        boolean didSomething = false;
        while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) {
            mExecutingActions = true;
            try {
                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
            } finally {
                cleanupExec();
            }
            didSomething = true;
        }

        doPendingDeferredStart();
        burpActive();

        return didSomething;
    }

接着就是removeRedundantOperationsAndExecute中执行executeOpsTogether,executeOpsTogether中执行executeOps
最后就来看下executeOps,已经找到真相了

    /**
     * Executes the operations contained within this transaction. The Fragment states will only
     * be modified if optimizations are not allowed.
     */
    void executeOps() {
        final int numOps = mOps.size();
        for (int opNum = 0; opNum < numOps; opNum++) {
            final Op op = mOps.get(opNum);
            final Fragment f = op.fragment;
            if (f != null) {
                f.setNextTransition(mTransition, mTransitionStyle);
            }
            switch (op.cmd) {
                case OP_ADD:
                    f.setNextAnim(op.enterAnim);
                    mManager.addFragment(f, false);
                    break;
                case OP_REMOVE:
                    f.setNextAnim(op.exitAnim);
                    mManager.removeFragment(f);
                    break;
                case OP_HIDE:
                    f.setNextAnim(op.exitAnim);
                    mManager.hideFragment(f);
                    break;
                case OP_SHOW:
                    f.setNextAnim(op.enterAnim);
                    mManager.showFragment(f);
                    break;
                case OP_DETACH:
                    f.setNextAnim(op.exitAnim);
                    mManager.detachFragment(f);
                    break;
                case OP_ATTACH:
                    f.setNextAnim(op.enterAnim);
                    mManager.attachFragment(f);
                    break;
                case OP_SET_PRIMARY_NAV:
                    mManager.setPrimaryNavigationFragment(f);
                    break;
                case OP_UNSET_PRIMARY_NAV:
                    mManager.setPrimaryNavigationFragment(null);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown cmd: " + op.cmd);
            }
            if (!mReorderingAllowed && op.cmd != OP_ADD && f != null) {
                mManager.moveFragmentToExpectedState(f);
            }
        }
        if (!mReorderingAllowed) {
            // Added fragments are added at the end to comply with prior behavior.
            mManager.moveToState(mManager.mCurState, true);
        }
    }

再来看看showFragment和hideFragment

    /**
     * Marks a fragment as hidden to be later animated in with
     * {@link #completeShowHideFragment(Fragment)}.
     *
     * @param fragment The fragment to be shown.
     */
    public void hideFragment(Fragment fragment) {
        if (DEBUG) Log.v(TAG, "hide: " + fragment);
        if (!fragment.mHidden) {
            fragment.mHidden = true;
            // Toggle hidden changed so that if a fragment goes through show/hide/show
            // it doesn't go through the animation.
            fragment.mHiddenChanged = !fragment.mHiddenChanged;
        }
    }
    /**
     * Marks a fragment as shown to be later animated in with
     * {@link #completeShowHideFragment(Fragment)}.
     *
     * @param fragment The fragment to be shown.
     */
    public void showFragment(Fragment fragment) {
        if (DEBUG) Log.v(TAG, "show: " + fragment);
        if (fragment.mHidden) {
            fragment.mHidden = false;
            // Toggle hidden changed so that if a fragment goes through show/hide/show
            // it doesn't go through the animation.
            fragment.mHiddenChanged = !fragment.mHiddenChanged;
        }
    }

这两个方法都对mHiddenChanged进行重新赋值,就导致了onHiddenChanged的回调。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342