Fragment预防预加载

public abstract class BaseFragment

extends Fragment { /** * 视图是否已经初初始化 */ public Activity mActivity; public P mPresenter; public M mModel; private Unbinder bind; public boolean isViewInited = false; public boolean isVisibleToUser = false; @Override public void onAttach(Context context) { mActivity = (Activity) context; super.onAttach(context); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(getFragmentLayoutId(), container, false); bind = ButterKnife.bind(this, inflate); init(); isViewInited = true; if (isViewInited && isVisibleToUser) initData(); return inflate; } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); this.isVisibleToUser = isVisibleToUser; Log.e("TAG",isVisibleToUser+"--"+this.getClass().getName()); if(isVisibleToUser&&isViewInited){ initData(); } } @Override public void onDestroy() { bind.unbind(); mActivity = null; super.onDestroy(); } private void init() { mPresenter = TUtils.getT(this, 0); mModel = TUtils.getT(this, 1); if (this instanceof BaseView) { mPresenter.setMv(mModel, this); } initView(); } protected abstract void initView(); protected abstract void initData(); public abstract int getFragmentLayoutId(); }

你可能感兴趣的:(Fragment预防预加载)