Giter VIP home page Giter VIP logo

testlazyfragment's Introduction

TestLazyFragment

通过测试fragment在activity和viewPager中的各种生命周期调用过程log打印,最终实现懒加载的LazyFragment

Fragment生命周期方法

前半段:
  • onAttach

  • onCreate

  • onCreateView

  • onViewCreated

  • onActivityCreated

  • onViewStateRestored

  • onStart

  • onResume

后半段:
  • onPause

  • onStop

  • onDestroyView

  • onDestroy

  • onDetach


  1. 如果调用了FragmentTransaction的showhide方法还会触发Fragment的onHiddenChanged(boolean hidden)
  2. 如果在ViewPager中使用FragmentPagerAdapter,关于setUserVisibleHint(boolean isVisibleToUser)的调用时机如下
    1. onAttach生命周期之前会调用并传入参数是false
    2. 如果是当前Fragment会重复再次调用并传入true。
    3. 切换ViewPager时,离开的fragment传入false、进入的fragment传入true
    4. Fragment嵌套的子Fragment不会调用setUserVisibleHint方法
  3. 在AndroidX下ViewPager使用 FragmentPagerAdapter(FragmentManager fm, int behavior),behavior参数传入FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT参数,控制只有当前fragment的生命周期会执行onResume,其它的fragment只会执行到onStart。并且setUserVisibleHint不再会调用

使用LazyFragment只需要关注以下4个API

/**
* 布局文件
*
* @return
*/
protected abstract int getLayoutResId()
/**
 * 懒加载的布局文件被加载了的回调函数,通常情况下当Fragment第一次可见的时候才回调此函数,
 * 也可以调用{@link #inflateLayout()}让Fragment创建后立即加载布局并回调此函数
 *
 * @param view
 */
protected void onLazyViewCreated(View view)
/**
 * 加载布局文件,调用了此方法的Fragment被创建后布局文件会被立即加载
 *
 * @return
 */
public LazyFragment inflateLayout()
/**
 * fragment可见状态改变的时候回调
 *
 * @param isVisible      是否可见
 * @param isFirstVisible 第一次可见为true,其它为false
 */
protected abstract void onLazyHiddenChanged(boolean isVisible, boolean isFirstVisible)

testlazyfragment's People

Watchers

James Cloos avatar 奔跑的蜗牛 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.