Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Inside LifecycleObserver

kobito-kaba
July 18, 2018
80

Inside LifecycleObserver

How to handle components' lifecycle in Android app developing.

kobito-kaba

July 18, 2018
Tweet

Transcript

  1. ×

  2. public class LifecycleRegistry extends Lifecycle { @Override public void addObserver(@NonNull

    LifecycleObserver observer) { State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED; ObserverWithState statefulObserver = new ObserverWithState(observer, initialState); ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
  3. public class LifecycleRegistry extends Lifecycle { @Override public void addObserver(@NonNull

    LifecycleObserver observer) { State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED; ObserverWithState statefulObserver = new ObserverWithState(observer, initialState); ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver);
  4. static class ObserverWithState { ... ObserverWithState(LifecycleObserver observer, State initialState) {

    mLifecycleObserver = Lifecycling.getCallback(observer); ... } void dispatchEvent(LifecycleOwner owner, Event event) { ... mLifecycleObserver.onStateChanged(owner, event); ... } }
  5. static class ObserverWithState { ... ObserverWithState(LifecycleObserver observer, State initialState) {

    mLifecycleObserver = Lifecycling.getCallback(observer); ... } void dispatchEvent(LifecycleOwner owner, Event event) { ... mLifecycleObserver.onStateChanged(owner, event); ... } }
  6. public class SupportActivity extends Activity implements LifecycleOwner { ... protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ReportFragment.injectIfNeededIn(this); } ... }
  7. public class ReportFragment extends Fragment { ... public void onActivityCreated(Bundle

    savedInstanceState) { dispatch(Lifecycle.Event.ON_CREATE); } public void onStart() { dispatch(Lifecycle.Event.ON_START); } public void onResume() { dispatch(Lifecycle.Event.ON_RESUME); } public void onPause() { dispatch(Lifecycle.Event.ON_PAUSE); } public void onStop() { dispatch(Lifecycle.Event.ON_STOP); } ... }
  8. public class ReportFragment extends Fragment { ... private void dispatch(Lifecycle.Event

    event) { ... if (activity instanceof LifecycleOwner) { Lifecycle lifecycle = ((LifecycleOwner) activity).getLifecycle(); if (lifecycle instanceof LifecycleRegistry) { ((LifecycleRegistry) lifecycle).handleLifecycleEvent(event); } } } }
  9. class NetworkStateReceiver(private val callback : (NetworkInfo?) -> Unit) : BroadcastReceiver()

    { override fun onReceive(context: Context?, intent: Intent?) { val conn = context?.getSystemService(ConnectivityManager::class.java) ?: return callback(conn.activeNetworkInfo) } } Step1 : Create a BroadcastReceiver
  10. receiver = NetworkStateReceiver { if (it != null && it.isConnected)

    { textView.text = "online" } else { textView.text = "offline" } } Step2 : create an instance create an instance
  11. Step3 : register it at onResume override fun onResume() {

    super.onResume() val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) registerReceiver(receiver, filter) } register at onResume
  12. override fun onPause() { super.onPause() unregisterReceiver(receiver) } Step5 : Never

    forget to unregister unregister at onPause Don’t miss it
  13. enum class Connectivity { OFFLINE, ONLINE } class NetworkObservable( private

    var activity: Activity?, private val callback : (Connectivity) -> Unit) : LifecycleObserver { private val receiver = NetworkStateReceiver { if (it != null && it.isConnected) { callback(Connectivity.ONLINE) } else { callback(Connectivity.OFFLINE) } } } Step1 : Wrap the receiver
  14. enum class Connectivity { OFFLINE, ONLINE } class NetworkObservable( private

    var activity: Activity?, private val callback : (Connectivity) -> Unit) : LifecycleObserver { private val receiver = NetworkStateReceiver { if (it != null && it.isConnected) { callback(Connectivity.ONLINE) } else { callback(Connectivity.OFFLINE) } } } Step1 : Wrap the receiver implements LifecycleObserver
  15. @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun registerReceiver() { activity?.registerReceiver(receiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun unregisterReceiver()

    { activity?.unregisterReceiver(receiver) } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun release() { activity = null } Step2 : register / unregister at lifecycle events
  16. @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun registerReceiver() { activity?.registerReceiver(receiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) } @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun unregisterReceiver()

    { activity?.unregisterReceiver(receiver) } @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun release() { activity = null } Step2 : register / unregister at lifecycle events @OnLifecycleEvent
  17. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    ... lifecycle.addObserver( NetworkObservable(this) { if (it == Connectivity.ONLINE) { textView.text = "online" } else { textView.text = "offline" } }) } } Step3 : Use it !
  18. class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {

    ... lifecycle.addObserver( NetworkObservable(this) { if (it == Connectivity.ONLINE) { textView.text = "online" } else { textView.text = "offline" } }) } } Step3 : Use it ! easy to use
  19. Lifecycle Management - is a hard and hassle task -

    can cause problems The Lifecycle library
  20. Lifecycle Management - is a hard and hassle task -

    can cause problems The Lifecycle library - provides the feature to observe lifecycles
  21. Lifecycle Management - is a hard and hassle task -

    can cause problems The Lifecycle library - provides the feature to observe lifecycles - can create lifecycle-aware components easily