Android Lifecycle使用

引言

Lifecycle 是官方提供的架構組件之一,目前已是穩定版本,Lifecycle 組件包括LifecycleOwner、LifecycleObserver。Lifecycle 組件是執行操做以響應另外一個組件(Activity或者Fragment)的生命週期狀態的更改。 Lifecycle 生成更易於組織、更輕量級,更易於維護的代碼。java

不使用Lifecycle

在使用MVP模式中,若是須要Presenter感知Activity或者Fragment的生命週期,傳統作法是Presenter中定義多個和Activity或者Fragment相應的生命週期方法,而後在Activity或者Fragment中調用Presenter中定義的方法,例如:android

class UserPresenter(view: IUserView) {
    private val mView = view
    private val mModel: UserModel = UserModel()

    fun onStart(){
        // 初始化一些信息
    }
    
    fun onDestroy(){
        // 釋放一塊兒請求
    }
}
class MainActivity : AppCompatActivity(), IUserView {

    private val userPresenter = UserPresenter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, MainFragment.newInstance())
                .commitNow()
        }
        
    }
    
    override fun onStart() {
        super.onStart()
        userPresenter.onStart()
    }


    override fun onDestroy() {
        super.onDestroy()
        userPresenter.onDestroy()
    }

    override fun onLoading() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onSuccess() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onComplete() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

實際項目中,需求可能比較複雜,這樣會致使太多相似的調用,而使onStart()和onDestroy() 方法變的很是臃腫。架構

使用Lifecycle

定義IPresenter 接口類,繼承LifecycleObserver ,其它具體的Presenter繼承該類,方便使用。app

interface IPresenter : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart(owner: LifecycleOwner)

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy(owner: LifecycleOwner)
}

定義一個具體的UserPresenter,繼承IPresenter框架

class UserPresenter(view: IUserView) : IPresenter {
    private val mView = view
    private val mModel: UserModel = UserModel()

    companion object {
        const val TAG: String = "UserPresenter"
    }

    override fun onStart(owner: LifecycleOwner) {
        Log.d(TAG,"onStart and Presenter")
    }

    override fun onDestroy(owner: LifecycleOwner) {
        Log.d(TAG,"onDestroy and Presenter")
    }
}

在Activity或者Fragment使用ide

class MainActivity : AppCompatActivity(), IUserView {

    companion object {
        val TAG: String = "MainActivity"
    }

    private val userPresenter = UserPresenter(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container, MainFragment.newInstance())
                .commitNow()
        }
        lifecycle.addObserver(userPresenter)// 訂閱事件
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG,"onStart and UI")
    }


    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG,"onDestroy and UI")
    }

    override fun onLoading() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onSuccess() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onComplete() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
}

運行查看下日誌ui

2018-12-15 12:39:50.715 2679-2679/com.fomin.arch D/MainActivity: onStart and UI
2018-12-15 12:39:50.715 2679-2679/com.fomin.arch D/UserPresenter: onStart and Presenter
2018-12-15 12:39:53.876 2679-2679/com.fomin.arch D/UserPresenter: onDestroy and Presenter
2018-12-15 12:39:53.877 2679-2679/com.fomin.arch D/MainActivity: onDestroy and UI

這樣Presenter 感知Activity或Fragment的生命週期已經實現,在Activity或Fragment生命週期變化會及時通知到Presenter。無需再Activity或Fragment實現相關Presenter的生命週期感知事件,減小維護成本。this

Lifecycle原理

首先了解下Lifecycle組件主要有下面一些關鍵類spa

  • LifecycleObserver :實現該接口的類,經過註解的方式,能夠經過被LifecycleOwner類的addObserver方法註冊,被註冊後,LifecycleObserver即可以觀察到LifecycleOwner的生命週期事件
  • LifecycleOwner:實現該接口的類持有生命週期(Lifecycle對象),該接口的生命週期(Lifecycle對象)的改變會被其註冊的觀察者LifecycleObserver觀察到並觸發其對應的事件
  • Event:從框架和Lifecycle類派發的生命週期事件
  • State :由Lifecycle對象跟蹤的組件的當前狀態
  • LifecycleRegistry:負責控制state的轉換、接受分發event事件

已Activity爲例,會發如今SupportActivity裏面繼承了LifecycleOwner,持有LifecycleRegistry類,而且在getLifecycle()返回LifecycleRegistry類,方便繼承類使用該類。日誌

@RestrictTo(LIBRARY_GROUP)
public class SupportActivity extends Activity implements LifecycleOwner {

    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);

    @Override
    @SuppressWarnings("RestrictedApi")
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ReportFragment.injectIfNeededIn(this);
    }

    @CallSuper
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mLifecycleRegistry.markState(Lifecycle.State.CREATED);
        super.onSaveInstanceState(outState);
    }

    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}

而在業務Activity中須要lifecycle.addObserver(LifecycleObserver)訂閱事件,這樣就能夠生效訂閱/通知事件。Event分發是經過哪裏進行分發的呢?能夠看下ReportFragment.injectIfNeededIn(this)這句代碼,ReportFragment對各個狀態使用dispatch進行事件分發,而後調用LifecycleRegistry.handleLifecycleEvent,其接受到分發的Event從而改變State。

ReportFragment代碼片斷
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    dispatchCreate(mProcessListener);
     (Lifecycle.Event.ON_CREATE);
}

@Override
public void onStart() {
    super.onStart();
    dispatchStart(mProcessListener);
    dispatch(Lifecycle.Event.ON_START);
}

@Override
public void onResume() {
    super.onResume();
    dispatchResume(mProcessListener);
    dispatch(Lifecycle.Event.ON_RESUME);
}

@Override
public void onPause() {
    super.onPause();
    dispatch(Lifecycle.Event.ON_PAUSE);
}

@Override
public void onStop() {
    super.onStop();
    dispatch(Lifecycle.Event.ON_STOP);
}

@Override
public void onDestroy() {
    super.onDestroy();
    dispatch(Lifecycle.Event.ON_DESTROY);
    // just want to be sure that we won't leak reference to an activity
    mProcessListener = null;
}

private void dispatch(Lifecycle.Event event) {
    Activity activity = getActivity();
    if (activity instanceof LifecycleRegistryOwner) {
        ((LifecycleRegistryOwner) activity).getLifecycle().handleLifecycleEvent(event);
        return;
    }

    if (activity instanceof LifecycleOwner) {
        Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle();
        if (lifecycle instanceof LifecycleRegistry) {
            ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event);
        }
    }
}


LifecycleRegistry代碼片斷
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
    State next = getStateAfter(event);
    moveToState(next);
}

private void moveToState(State next) {
    if (mState == next) {
        return;
    }
    mState = next;
    if (mHandlingEvent || mAddingObserverCounter != 0) {
        mNewEventOccurred = true;
        // we will figure out what to do on upper level.
        return;
    }
    mHandlingEvent = true;
    sync();
    mHandlingEvent = false;
}
// happens only on the top of stack (never in reentrance),
// so it doesn't have to take in account parents
private void sync() {
    LifecycleOwner lifecycleOwner = mLifecycleOwner.get();
    if (lifecycleOwner == null) {
        Log.w(LOG_TAG, "LifecycleOwner is garbage collected, you shouldn't try dispatch "
                + "new events from it.");
        return;
    }
    while (!isSynced()) {
        mNewEventOccurred = false;
        // no need to check eldest for nullability, because isSynced does it for us.
        if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) {
            backwardPass(lifecycleOwner);
        }
        Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest();
        if (!mNewEventOccurred && newest != null
                && mState.compareTo(newest.getValue().mState) > 0) {
            forwardPass(lifecycleOwner);
        }
    }
    mNewEventOccurred = false;
}

能夠看下官方的對整個生命週期流動的解釋

在該Lifecycle組件中,與傳統的生命週期不一樣,只定義了五種狀態,分別是:

  • INITIALIZED 最初始的狀態
  • DESTROYED
  • CREATED
  • STARTED
  • RESUMED

上圖中向右的箭頭很好理解,每過來一個event會發生生命週期狀態的變動,向左的箭頭能夠當作狀態的回滾, 若是在RESUMED狀態發生了ON_PAUSE事件,則狀態回滾到STARTED狀態;STARTED狀態發生了ON_STOP事件,則回滾到CREATED狀態。

總結

須要注意了是,在繼承LifecycleObserver 時,官方建議使用DefaultLifecycleObserver ,由於隨着Java8成爲主流,將不會再使用註解方式,DefaultLifecycleObserver是須要另外聲明的java8,而且最低API24才能build success。而GenericLifecycleObserver,FullLifecycleObserver,DefaultLifecycleObserver 這三個接口都是直接或者間接繼承的LifecycleObserver。

// java8的lifecycle引用
implementation "android.arch.lifecycle:common-java8:1.1.1"
// 註解方式lifecycle引用
implementation 'android.arch.lifecycle:extensions:1.1.1'

Lifecycle使用,保持 UI 控制器(Activity 和 Fragment)儘量的精簡,更易複用,同時和LiveDataViewModel等使用更具備最佳實戰。

相關文章
相關標籤/搜索