1. 程式人生 > >Android官方架構組件:Lifecycle詳解&迪士尼彩樂園定制開發原理分析

Android官方架構組件:Lifecycle詳解&迪士尼彩樂園定制開發原理分析

npr save this end ons 關於 直接 能夠 封裝

Lifecycle 是一個類,它持有關於組件(如 Activity 或 Fragment)生命周期狀態的信息,並且允許其他對象觀察此狀態。

我們只需要2步:

1、Prestener繼承LifecycleObserver接口
public interface IPresenter extends LifecycleObserver {

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
void onCreate(@NotNull LifecycleOwner owner);

@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
void onDestroy(@NotNull LifecycleOwner owner);

@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onLifecycleChanged(@NotNull LifecycleOwner owner,
                        @NotNull Lifecycle.Event event);

}
Android官方架構組件:Lifecycle詳解&原理分析
迪士尼彩樂園定制開發,需要請搜索【大神源碼論壇】dsluntan.com 客服企娥3393756370 V信17061863513,

public class BasePresenter implements IPresenter {

private static final String TAG = "com.qingmei2.module.base.BasePresenter";    

@Override
public void onLifecycleChanged(@NotNull LifecycleOwner owner, @NotNull Lifecycle.Event event) {

}

@Override
public void onCreate(@NotNull LifecycleOwner owner) {
    Log.d("tag", "BasePresenter.onCreate" + this.getClass().toString());
}

@Override
public void onDestroy(@NotNull LifecycleOwner owner) {
    Log.d("tag", "BasePresenter.onDestroy" + this.getClass().toString());
}

}

這裏我直接將我想要觀察到Presenter的生命周期事件都列了出來,然後封裝到BasePresenter 中,這樣每一個BasePresenter 的子類都能感知到Activity容器對應的生命周期事件,並在子類重寫的方法中,對應相應行為。

2、在Activity/Fragment容器中添加Observer:
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
    }

Android官方架構組件:Lifecycle詳解&迪士尼彩樂園定制開發原理分析