关于onhiddenchanged和setUserVisibleHint函数的知识
/** * 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 if it is not * visible. */ onhiddenchanged(boolean hidden)
v4包下的Fragment里面的文档,大概意思就是:当你用FragmentTransaction来控制fragment的hide和show时,那么这个方法就会被调用。每当你对某个Fragment使用hide或者是show的时候,那么这个Fragment就会自动调用这个方法。 (使用情况:你自己去管理Fragment,而不是用viewpager管理的时候)
/** * Set a hint to the system about whether this fragments 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 fragments 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> * * @param isVisibleToUser true if this fragments UI is currently visible to the user (default), * false if it is not. */ setUserVisibleHint(boolean isVisibleToUser)
比如说当我们在使用viewpager的时候,viewpager内部有个提前缓存的机制(默认是提前缓存一页),比如你在看第一个Fragment的时候,隔壁的Fragment已经创建好了,但此时的状态却是不可见的。 但是这时候Fragment不会去调用上面说的onhiddenchanged方法,只会调用setUserVisibleHint这个方法。