源码学习之Fragment

默认的构造函数,每个fragment必须有一个空的构造函数,当恢复activity状态的时候可以被实例化,强烈建议子类不要有有参构造函数,在fragment重新被实例化的时候不会调用这些构造函数。可以设置通过setArguments设置Bundle数据,通过getArguments取出数据。
public Fragment() {
}
根据class name创建Fragment实例,在这个方法里 也会调用fragment的无参构造函数,如果args不等于null,给mArguments 赋值,可以通过getArguments()获取。
public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
        try {
            Class<?> clazz = sClassMap.get(fname);//先从sClassMap缓存里查找对应的class,如果没有,通过classloader去加载,并放入缓存
            if (clazz == null) {
                // Class not found in the cache, see if it's real, and try to add it
                clazz = context.getClassLoader().loadClass(fname);
                sClassMap.put(fname, clazz);
            }
            Fragment f = (Fragment)clazz.newInstance();//调用无参构造函数创建一个实例
            if (args != null) {
                args.setClassLoader(f.getClass().getClassLoader());
                f.mArguments = args;//赋值
            }
            return f;
        } catch (ClassNotFoundException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        } catch (java.lang.InstantiationException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        } catch (IllegalAccessException e) {
            throw new InstantiationException("Unable to instantiate fragment " + fname
                    + ": make sure class name exists, is public, and has an"
                    + " empty constructor that is public", e);
        }
    }
调用这个方法来保存实例状态,如果fragment会被重新创建,outState会被传入onCreate(Bundle)onCreateView(LayoutInflater, ViewGroup, Bundle)onActivityCreated(Bundle)中。
这个方法相当于Activity的onSaveInstanceState(Bundle)方法。这个方法可以在onDestroy()之前的任一时刻被调用,有很多情况都可以导致fragment被销毁(例如fragment被放到回退栈的时候),它的状态不会被保存,除非activity需要保存。
public void onSaveInstanceState(Bundle outState) {
}
状态恢复,
final void restoreViewState(Bundle savedInstanceState) {
        if (mSavedViewState != null) {
            mInnerView.restoreHierarchyState(mSavedViewState);
            mSavedViewState = null;
        }
        mCalled = false;
        onViewStateRestored(savedInstanceState);//子类可以重写这个方法来进行数据的恢复,必须调用父类的onViewStateRestored方法。
        if (!mCalled) {
            throw new SuperNotCalledException("Fragment " + this
                    + " did not call through to super.onViewStateRestored()");
        }
    }
用来管理fragment内部的fragment。
final public FragmentManager getChildFragmentManager() {
}
如果fragment添加到activity,返回truefinal public boolean isAdded() {
     return mHost != null && mAdded;
}
FragmentTransaction.detach(Fragment)调用这个方法之后,fragment从用户界面分离,返回truefinal public boolean isDetached() {
        return mDetached;
    }
fragment是否可见,条件是:1.被添加到activity,2.view被添加到了window,3.没有被隐藏,
final public boolean isVisible() {
        return isAdded() && !isHidden() && mView != null
                && mView.getWindowToken() != null && mView.getVisibility() == View.VISIBLE;
    }
fragment是否隐藏。fragment默认是显示的。可以通过onHiddenChanged方法来处理状态的改变。
final public boolean isHidden() {
        return mHidden;
    }
当fragment第一次和context进行关联的时候被调用。
public void onAttach(Context context) {
}
这个方法在onAttach()之后被调用,在onCreateView(LayoutInflater, ViewGroup, Bundle)之前被调用。
这个方法可能在fragment的Activity被创建的过程中调用,所以不能使用activity的视图层。可以在Activity被创建之后做一些工作。
public void onCreate(@Nullable Bundle savedInstanceState) {
        mCalled = true;
    }
调用这个方法来实例化fragment的视图。这个方法在onCreate(Bundle)、onActivityCreated(Bundle)中间调用。如果这个方法返回了view,当view被释放的时候会调用onDestroyView。
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        return null;
    }
当fragment的activity被创建,fragment的视图层被实例化之后会调用这个方法。在这里可以做最终的初始化,例如检索视图或恢复状态。
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    mCalled = true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值