1. 程式人生 > >Android 官方體系架構Lifecycle

Android 官方體系架構Lifecycle

前言

   Google I/O 2017提出的Android新體系架構Lifecycle其根本目的是實現Activity和Fragment的生命週期與邏輯的解耦,也可以在自定義View中實現監聽Activity或者Fragment的生命週期(注意你獲取的Lifecycle物件來自哪裡就是監聽的哪一個Activity或者Fragment).在MVP架構中我們就可以實現在Presenter中監聽Activity和Fragment的生命週期,這樣也就做到了解耦合.

Lifecycle觀察的幾種狀態

  • ON_CREATE:關聯onCreate事件
  • ON_START:關聯onStart事件
  • ON_RESUME:關聯onResume事件
  • ON_PAUSE:關聯onPause事件
  • ON_STOP:關聯onStop事件
  • ON_DESTROY:關聯onDestroy事件
  • ON_ANY:關聯所有的生命週期的事件,可用於檢查狀態改變

Lifecycle相關的類

  • Lifecycle 用於新增或移除觀察物件,獲取當前狀態,且包含了觀察狀態的列舉類
  • LifecycleOwner 一個介面,只含有一個方法獲取Lifecycle物件的方法
  • LifecycleObserver 一個介面,其本身不含有任何方法,只是用於標記這個類是一個生命週期的觀察類
  • LifecycleRegistry 用於自定義一個生命週期的觀察類

Lifecycle的使用

(1)建立一個介面實現LifecycleObserver

public interface IPresenter extends LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    default void OnCreate(@NonNull LifecycleOwner owner){}

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    default void OnDestroy(@NonNull LifecycleOwner owner){}

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    default void OnPause(@NonNull LifecycleOwner owner){}

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    default void OnResume(@NonNull LifecycleOwner owner){}

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    default void OnStart(@NonNull LifecycleOwner owner){}

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    default void OnStop(@NonNull LifecycleOwner owner){}

    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    default void OnLifeCycleChanged(@NonNull LifecycleOwner owner){}
}

(2)建立一個IPresenter的實現類

public class PresenterImpl implements IPresenter {

    private String TAG = "Test";
    private String Name;

    public PresenterImpl(String name){
        this.Name = name;
    }

    @Override
    public void OnCreate(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnCreate");
    }

    @Override
    public void OnDestroy(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnDestroy");
    }

    @Override
    public void OnPause(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnPause");
    }

    @Override
    public void OnResume(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnResume");
    }

    @Override
    public void OnStart(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnStart");
    }

    @Override
    public void OnStop(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnStop");
    }

    @Override
    public void OnLifeCycleChanged(@NonNull LifecycleOwner owner) {
        Log.d(TAG,Name+":OnLifeCycleChanged");
    }
}

(3)在Presenter中監聽Activity的生命週期

public class MainActivity extends AppCompatActivity {

    private IPresenter mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPresenter = new PresenterImpl("MainActivity");
        getLifecycle().addObserver(mPresenter);

    }
}

(4)在Presenter中監聽Fragment的生命週期

public class MFragment extends Fragment {
    private IPresenter mPresenter;
    private View mRootView;
    private MTextView mTextView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(mRootView==null){
            mRootView = inflater.inflate(R.layout.fragment_test,container,false);
            mPresenter = new PresenterImpl("MFragment");
            getLifecycle().addObserver(mPresenter);
            mTextView = mRootView.findViewById(R.id.textView);
            mTextView.setLifecycle(getLifecycle());
        }
        return mRootView;
    }
}

(5)在自定義View中監聽Activity或者Fragment的生命週期 這裡我們通過setLifecycle方法為View設定監聽(這裡的lifecycle如果是來自Activity的則監聽的是該Activity的生命週期,如果來自Fragment則是監聽該Fragment的生命週期)

public class MTextView extends AppCompatTextView implements IPresenter {
    
    private String TAG = "Test";

    public MTextView(Context context) {
        this(context,null);
    }

    public MTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void setLifecycle(Lifecycle lifecycle){
        lifecycle.addObserver(this);
    }

    @Override
    public void OnCreate(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnCreate");
    }

    @Override
    public void OnDestroy(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnDestroy");
    }

    @Override
    public void OnPause(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnPause");
    }

    @Override
    public void OnResume(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnResume");
    }

    @Override
    public void OnStart(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnStart");
    }

    @Override
    public void OnStop(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnStop");
    }

    @Override
    public void OnLifeCycleChanged(@NonNull LifecycleOwner owner) {
        Log.d(TAG,"OnLifeCycleChanged");
    }
}

原始碼地址