Fragment懒加载

我们在做应用开发的时候,一个Activity里面可能会以viewpager(或其他容器)与多个Fragment来组合使用,而如果每个fragment都需要去加载数据,或从本地加载,或从网络加载,那么在这个activity刚创建的时候就变成需要初始化大量资源。这样的结果,我们当然不会满意。那么,能不能做到当切换到这个fragment的时候,它才去初始化呢?

答案就在Fragment里的setUserVisibleHint这个方法里。请看关于Fragment里这个方法的API文档(国内镜像地址:Fragment api):

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.
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.
Parameters
isVisibleToUser true if this fragment’s UI is currently visible to the user (default), false if it is not.

该方法用于告诉系统,这个Fragment的UI是否是可见的。所以我们只需要继承Fragment并重写该方法,即可实现在fragment可见时才进行数据加载操作,即Fragment的懒加载。
代码如下:

/*
* Date: 14-7-17
* Project: Access-Control-V2
*/
package cn.irains.access_control_v2.common;
import android.support.v4.app.Fragment;
/**
* Author: msdx (645079761@qq.com)
* Time: 14-7-17 下午5:46
*/
public abstract class LazyFragment extends Fragment {
protected boolean isVisible;
/**
* 在这里实现Fragment数据的缓加载.
* @param isVisibleToUser
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(getUserVisibleHint()) {
isVisible = true;
onVisible();
} else {
isVisible = false;
onInvisible();
}
}
protected void onVisible(){
lazyLoad();
}
protected abstract void lazyLoad();
protected void onInvisible(){}
}
在LazyFragment,我增加了三个方法,一个是onVisiable,即fragment被设置为可见时调用,一个是onInvisible,即fragment被设置为不可见时调用。另外再写了一个lazyLoad的抽象方法,该方法在onVisible里面调用。你可能会想,为什么不在getUserVisibleHint里面就直接调用呢?

我这么写是为了代码的复用。因为在fragment中,我们还需要创建视图(onCreateView()方法),可能还需要在它不可见时就进行其他小量的初始化操作(比如初始化需要通过AIDL调用的远程服务)等。而setUserVisibleHint是在onCreateView之前调用的,那么在视图未初始化的时候,在lazyLoad当中就使用的话,就会有空指针的异常。而把lazyLoad抽离成一个方法,那么它的子类就可以这样做:

public class OpenResultFragment extends LazyFragment{
// 标志位,标志已经初始化完成。
private boolean isPrepared;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LOG_TAG, “onCreateView”);
View view = inflater.inflate(R.layout.fragment_open_result, container, false);
//XXX初始化view的各控件
isPrepared = true;
lazyLoad();
return view;
}
@Override
protected void lazyLoad() {
if(!isPrepared || !isVisible) {
return;
}
//填充各控件的数据
}
}
在上面的类当中,我们增加了一个标志位isPrepared,用于标志是否初始化完成。然后在我们所需要的初始化操作完成之后调用,如上面的例子当中,在初始化view之后,设置 isPrepared为true,同时调用lazyLoad()方法。而在lazyLoad()当中,判断isPrepared和isVisible只要有一个不为true就不往下执行。也就是仅当初始化完成,并且可见的时候才继续加载,这样的避免了未初始化完成就使用而带来的问题。

在这里我对fragment的懒加载实现的介绍就到此为止,如果你有兴趣,可以基于此再深入探究,比如写一个带有缓初始化和可见时刷新的特性的Fragment。

二:非中断保存

首先,要明确什么叫“非中断保存”。熟悉Fragment的开发人员都知道,Fragment是依附于Activity的。当Activity销毁时,Fragment会随之销毁。而当Activity配置发生改变(如屏幕旋转)时候,旧的Activity会被销毁,然后重新生成一个新屏幕旋转状态下的Activity,自然而然的Fragment也会随之销毁后重新生成,而新生成的Fragment中的各个对象也与之前的那个Fragment不一样,伴随着他们的动作、事件也都不一样。所以,这时候如果想保持原来的Fragment中的一些对象,或者想保持他们的动作不被中断的话,就迫切的需要将原来的Fragment进行非中断式的保存。

生命周期
Activity的生命周期在配置发生改变时:

onPuase->onStop->onDestroy->onStart->onResume
比如在Activity中发生屏幕旋转,其生命周期就是如此。而在onDestroy中,Activity会将其FragmentManager所包含的Fragment都销毁掉(默认状态),即Fragment的生命周期为:

onDestroyView->onDestroy->onDetach
通过查看FragmentManager.java的代码,可以发现在Fragment生命周期执行到onDestroyView时候,状态会由正常的ACTIVITY_CREATED变为CREATED。而到了onDestroy生命周期时候,执行的代码出现了有意思的事情:

if (!f.mRetaining) {
f.performDestroy();
}
f.mCalled = false;
f.onDetach();
if (!f.mCalled) {
throw new SuperNotCalledException(“Fragment ” + f
+ ” did not call through to super.onDetach()”);
}
if (!keepActive) {
if (!f.mRetaining) {
makeInactive(f);
} else {
f.mActivity = null;
f.mParentFragment = null;
f.mFragmentManager = null;
}
}
来源: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/FragmentManager.java
当Fragment的mRetaining被置true的时候,Destroy生命周期并不会执行,而Fragment的mRetaining状态是通过其retainNonConfig()来配置的,配置条件是Fragment不为空且Framgnet的mRetainInstance为true。到这里就能看到,如果想要自己的Fragment不被销毁掉,就要让这个mRetainInstance为true。

通过查阅Fragment.java源码发现,通过API setRetainInstance和getRetainInstance可以对其进行操作。同样,Android文档中对这两个接口也有了一定的描述。

总结
这里结合Fragment.java中setRetainInstance的注释进行一下Fragment非中断保存的总结。原注释如下:

/**
* Control whether a fragment instance is retained across Activity
* re-creation (such as from a configuration change). This can only
* be used with fragments not in the back stack. If set, the fragment
* lifecycle will be slightly different when an activity is recreated:
*


  • *
  • {@link #onDestroy()} will not be called (but {@link #onDetach()} still
    * will be, because the fragment is being detached from its current activity).
    *
  • {@link #onCreate(Bundle)} will not be called since the fragment
    * is not being re-created.
    *
  • {@link #onAttach(Activity)} and {@link #onActivityCreated(Bundle)} will
    * still be called.
    *

*/
public void setRetainInstance(boolean retain) {
if (retain && mParentFragment != null) {
throw new IllegalStateException(
“Can’t retain fragements that are nested in other fragments”);
}
mRetainInstance = retain;
}
如果想叫自己的Fragment即使在其Activity重做时也不进行销毁那么就要设置setRetainInstance(true)。进行了这样的操作后,一旦发生Activity重组现象,Fragment会跳过onDestroy直接进行onDetach(界面消失、对象还在),而Framgnet重组时候也会跳过onCreate,而onAttach和onActivityCreated还是会被调用。需要注意的是,要使用这种操作的Fragment不能加入backstack后退栈中。并且,被保存的Fragment实例不会保持太久,若长时间没有容器承载它,也会被系统回收掉的。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值