DevSupport随笔

android lifecycle介绍及实践

2019-09-17  本文已影响0人  成虫_62d0
allprojects {
    repositories {
        jcenter()
        google()//添加Google Maven仓库
    }
}
dependencies {
    // Java8 support for Lifecycles
    implementation "android.arch.lifecycle:common-java8:1.1.1"
// Lifecycles only (no ViewModel or LiveData)
    implementation "android.arch.lifecycle:runtime:1.1.1"
    annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
}

懒加载fragment相关的生命周期接口

public interface CoinSignalLifecycleObserver extends DefaultLifecycleObserver {

    default void onFragmentFirstVisible(){}

    default void onFragmentResume(){}

    default void onFragmentPause(){}

}

presenter接口

public interface BasePresenter extends CoinSignalLifecycleObserver {

    default void disPose(Disposable disposable) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }
}

fragment的实现

public class BaseMvpFragment<T extends BasePresenter> extends Fragment{

    protected T presenter;

    protected void initPresenter(){}

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresenter();
        if (presenter != null) {
            getLifecycle().addObserver(presenter);
        }
    }
}

Activity的实现,其中activity里有沉浸式相关代码

public abstract class BaseMvpActivity<T extends BasePresenter> extends AppCompatActivity implements LifecycleOwner {
    protected LifecycleRegistry mLifecycleRegistry;
    protected T presenter;

    protected ImmersiveHelper immersiveHelper;
    protected View statusView;

    protected abstract void initPresenter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLifecycleRegistry = new LifecycleRegistry(this);
        if (presenter != null) {
            getLifecycle().addObserver(presenter);
        }
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        setLayout();
        statusView = findViewById(R.id.status_bar_view);
        if (statusView != null) {
            immersiveHelper = new ImmersiveHelper(this, statusView);
        }
    }

    protected abstract void setLayout();

    @Override
    protected void onStart() {
        super.onStart();
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        if (immersiveHelper == null){
            return;
        }
        immersiveHelper.destroy();
        immersiveHelper = null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
        MobclickAgent.onPause(this);
    }

    /**
     * 是否可以使用沉浸式
     * Is immersion bar enabled boolean.
     *
     * @return the boolean
     */
    public boolean isImmersionBarEnabled() {
        return immersiveHelper.isImmersionBarEnabled();
    }

    public void initImmersionBar() {
        //在BaseActivity里初始化
        immersiveHelper.initImmersionBar();
    }

    public void hideSoftKeyBoard() {
        if (immersiveHelper == null){
            return;
        }
        immersiveHelper.hideSoftKeyBoard();
    }

    public void finish() {
        super.finish();
        hideSoftKeyBoard();
    }
}
上一篇下一篇

猜你喜欢

热点阅读