/* * Copyright 2017 JessYan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.jess.arms.base; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import com.jess.arms.base.delegate.IFragment; import com.jess.arms.integration.cache.Cache; import com.jess.arms.integration.cache.CacheType; import com.jess.arms.integration.lifecycle.FragmentLifecycleable; import com.jess.arms.mvp.IPresenter; import com.jess.arms.utils.ArmsUtils; import com.trello.rxlifecycle2.android.FragmentEvent; import javax.inject.Inject; import io.reactivex.subjects.BehaviorSubject; import io.reactivex.subjects.Subject; /** * ================================================ * 因为 Java 只能单继承, 所以如果要用到需要继承特定 @{@link Fragment} 的三方库, 那你就需要自己自定义 @{@link Fragment} * 继承于这个特定的 @{@link Fragment}, 然后再按照 {@link BaseFragment} 的格式, 将代码复制过去, 记住一定要实现{@link IFragment} * * @see <a href="https://github.com/JessYanCoding/MVPArms/wiki">请配合官方 Wiki 文档学习本框架</a> * @see <a href="https://github.com/JessYanCoding/MVPArms/wiki/UpdateLog">更新日志, 升级必看!</a> * @see <a href="https://github.com/JessYanCoding/MVPArms/wiki/Issues">常见 Issues, 踩坑必看!</a> * @see <a href="https://github.com/JessYanCoding/ArmsComponent/wiki">MVPArms 官方组件化方案 ArmsComponent, 进阶指南!</a> * Created by JessYan on 22/03/2016 * <a href="mailto:[email protected]">Contact me</a> * <a href="https://github.com/JessYanCoding">Follow me</a> * ================================================ */ public abstract class BaseFragment<P extends IPresenter> extends Fragment implements IFragment, FragmentLifecycleable { protected final String TAG = this.getClass().getSimpleName(); private final BehaviorSubject<FragmentEvent> mLifecycleSubject = BehaviorSubject.create(); protected Context mContext; @Inject @Nullable protected P mPresenter;//如果当前页面逻辑简单, Presenter 可以为 null private Cache<String, Object> mCache; @NonNull @Override public synchronized Cache<String, Object> provideCache() { if (mCache == null) { //noinspection unchecked mCache = ArmsUtils.obtainAppComponentFromContext(getActivity()).cacheFactory().build(CacheType.FRAGMENT_CACHE); } return mCache; } @NonNull @Override public final Subject<FragmentEvent> provideLifecycleSubject() { return mLifecycleSubject; } @Override public void onAttach(Context context) { super.onAttach(context); mContext = context; } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return initView(inflater, container, savedInstanceState); } @Override public void onDestroy() { super.onDestroy(); if (mPresenter != null) { mPresenter.onDestroy();//释放资源 } this.mPresenter = null; } @Override public void onDetach() { super.onDetach(); mContext = null; } /** * 是否使用 EventBus * Arms 核心库现在并不会依赖某个 EventBus, 要想使用 EventBus, 还请在项目中自行依赖对应的 EventBus * 现在支持两种 EventBus, greenrobot 的 EventBus 和畅销书 《Android源码设计模式解析与实战》的作者 何红辉 所作的 AndroidEventBus * 确保依赖后, 将此方法返回 true, Arms 会自动检测您依赖的 EventBus, 并自动注册 * 这种做法可以让使用者有自行选择三方库的权利, 并且还可以减轻 Arms 的体积 * * @return 返回 {@code true} (默认为 {@code true}), Arms 会自动注册 EventBus */ @Override public boolean useEventBus() { return true; } }