在Activity/Fragment容器中添加ObserverSSC平台架设

在Activity/Fragment容器中添加ObserverSSC平台架设,需要请搜索【大神源码论坛】dsluntan.com 客服企娥3393756370 V信17061863513,
public class MainActivity extends AppCompatActivity {
private IPresenter mPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d("tag", "onCreate" + this.getClass().toString());
    setContentView(R.layout.activity_main);
    mPresenter = new MainPresenter(this);
    getLifecycle().addObserver(mPresenter);//添加LifecycleObserver
}

@Override
protected void onDestroy() {
    Log.d("tag", "onDestroy" + this.getClass().toString());
    super.onDestroy();
}

}

如此,每当Activity发生了对应的生命周期改变,Presenter就会执行对应事件注解的方法:

除onCreate和onDestroy事件之外,Lifecycle一共提供了所有的生命周期事件,只要
通过注解进行声明,就能够使LifecycleObserver观察到对应的生命周期事件:

//以下为logcat日志
01-08 23:21:01.702 D/tag: onCreate class com.qingmei2.mvparchitecture.mvp.ui.MainActivity
01-08 23:21:01.778 D/tag: onCreate class com.qingmei2.mvparchitecture.mvp.presenter.MainPresenter

01-08 23:21:21.074 D/tag: onDestroy class com.qingmei2.mvparchitecture.mvp.presenter.MainPresenter
01-08 23:21:21.074 D/tag: onDestroy class com.qingmei2.mvparchitecture.mvp.ui.MainActivity

public enum Event {
/**

  • Constant for onCreate event of the {@link LifecycleOwner}.
    */
    ON_CREATE,
    /**
  • Constant for onStart event of the {@link LifecycleOwner}.
    */
    ON_START,
    /**
  • Constant for onResume event of the {@link LifecycleOwner}.
    */
    ON_RESUME,
    /**
  • Constant for onPause event of the {@link LifecycleOwner}.
    */
    ON_PAUSE,
    /**
  • Constant for onStop event of the {@link LifecycleOwner}.
    */
    ON_STOP,
    /**
  • Constant for onDestroy event of the {@link LifecycleOwner}.
    */
    ON_DESTROY,
    /**
  • An {@link Event Event} constant that can be used to match all events.
    */
    ON_ANY
    }

猜你喜欢

转载自blog.51cto.com/13974315/2175957