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

Architecture Components 勉強会 第1回目 Lifecycles

D2bcabeeb1ddff142fb8988b412cb4d3?s=47 Yuki Anzai
December 20, 2017

Architecture Components 勉強会 第1回目 Lifecycles

D2bcabeeb1ddff142fb8988b412cb4d3?s=128

Yuki Anzai

December 20, 2017
Tweet

Transcript

  1. Architecture Components ษڧձ ͋Μ͍͟Ώ͖ ͕͑Θ Θ͞ͼʔ; ͋Β͖ ୈ1ճ໨ Lifecycles

  2. https://goo.gl/5BjmG5

  3. Download Android Studio 3.0 https://goo.gl/KmYZzW

  4. Architecture Components Lifecycles LiveData ViewModel Room Paging

  5. Architecture Components Lifecycles LiveData ViewModel Room Paging ݸผར༻OK

  6. Architecture Components Lifecycles LiveData ViewModel Room Paging ݸผར༻OK ૊Έ߹Θͤར༻OK

  7. Architecture Components Lifecycles LiveData ViewModel Room Paging ࠓ೔ͷςʔϚ͸͜Ε

  8. Architecture Components Google I/O 2017 1.0.0 alpha 1 : 1.0.0

    2017 11 6 Paging : 1.0.0-alpha4-1 2017 12 11 https://developer.android.com/topic/libraries/architecture/release-notes.html
  9. Architecture Components (robust) (testable (maintainable)

  10. Architecture Components allprojects { repositories { google() jcenter() } }

    ϓϩδΣΫτͷ build.gradle maven { url 'https://maven.google.com' } Android Gradle Plugin ͕ 3.0 ະຬͳΒ https://developer.android.com/topic/libraries/architecture/adding-components.html
  11. Lifecycles

  12. Lifecycles Ϟδϡʔϧͷ build.gradle dependencies { implementation "android.arch.lifecycle:runtime:1.0.3" annotationProcessor "android.arch.lifecycle:compiler:1.0.0" }

    * Java8 Language Support ʹ͍ͭͯ͸ࠓճ͸औΓ্͛·ͤΜ https://developer.android.com/topic/libraries/architecture/adding-components.html
  13. v4 support library 26.1.0 Lifecycle FragmentActivity, Fragment v4 support library

    android.arch.lifecycle:runtime Lifecycle, LifecycleOwner, LifecycleObserver, LifecycleRegistry, GenericLifecycleObserver, OnLifecycleEvent * LifecycleRegistryOwner ͱ LifecycleActivity ͱ LifecycleFragment ͸ deprecated ʹͳ͍ͬͯΔ
  14. Lifecycles Lifecycles = Lifecycle-aware Components Activity Fragment

  15. class & interface

  16. Lifecycle Lifecycles (Lifecycle.State) (Lifecycle.Event)

  17. final Lifecycle.State state = lifecycle.getCurrentState();

  18. Lifecycle.State enum INITIALIZED DESTROYED CREATED STARTED RESUMED https://developer.android.com/topic/libraries/architecture/lifecycle.html

  19. Lifecycle.Event enum https://developer.android.com/topic/libraries/architecture/lifecycle.html ON_CREATE ON_START ON_RESUME ON_PAUSE ON_STOP ON_DESTROY ON_ANY

  20. xx final boolean isAtLeastResumed = lifecycle.getCurrentState().isAtLeast(Lifecycle.State.RESUMED);

  21. LifecycleOwner Lifecycle single method interface FragmentActivity LifecycleOwner v4 support library

    Fragment LifecycleOwner public interface LifecycleOwner { @NonNull Lifecycle getLifecycle(); }
  22. package android.support.v4.app; @RestrictTo(LIBRARY_GROUP) public class SupportActivity extends Activity implements LifecycleOwner

    { … @Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } } @RequiresApi(14) abstract class BaseFragmentActivityApi14 extends SupportActivity { @RestrictTo(LIBRARY_GROUP) abstract class BaseFragmentActivityApi16 extends BaseFragmentActivityApi14 { public class FragmentActivity extends BaseFragmentActivityApi16 implements ActivityCompat.OnRequestPermissionsResultCallback, ActivityCompat.RequestPermissionsRequestCodeValidator { … @Override public Lifecycle getLifecycle() { return super.getLifecycle(); } } SupportActivity Ͱ LifecycleRegistry Λ ࢖࣮ͬͯ૷͞Ε͍ͯΔ
  23. FragmentActivity Lifecycle public class MainActivity extends AppCompatActivity { @Override protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Lifecycle lifecycle = getLifecycle(); }
  24. LifecycleObserver Lifecycle interface @OnLifecycleEvent Lifecycle.Event public interface LifecycleObserver { }

  25. OnLifecycleEvent LifecycleObserver LifecycleEvent ON_CREATE, ON_START, ON_RESUME, ON_PAUSE, ON_STOP, ON_DESTROY, ON_ANY

    final LifecycleObserver observer = new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_START) public void calledWhenStart() { } };
  26. OnLifecycleEvent final LifecycleObserver observer = new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_START) public

    void calledWhenOnStart() { Log.d("Lifecycle", "ON_START"); } @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void calledWhenOnAny() { Log.d("Lifecycle", "ON_ANY"); } };
  27. OnLifecycleEvent 1 LifecycleOwner final LifecycleObserver observer = new LifecycleObserver() {

    @OnLifecycleEvent(Lifecycle.Event.ON_START) public void startSomething(LifecycleOwner source) { } };
  28. OnLifecycleEvent ON_ANY 2 Lifecycle.Event final LifecycleObserver observer = new LifecycleObserver()

    { @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { } };
  29. GenericLifecycleObserver LifecycleObserver interface Lifecycle.Event onStateChanged() public interface GenericLifecycleObserver extends LifecycleObserver

    { /** * Called when a state transition event happens. * * @param source The source of the event * @param event The event */ void onStateChanged(LifecycleOwner source, Lifecycle.Event event); } GenericLifecycleObserver @hide @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
  30. LifecycleRegistry Observer Lifecycle LifecycleOwner FragmentActivity, v4 support library Fragment @RestrictTo(LIBRARY_GROUP)

    public class SupportActivity extends Activity implements LifecycleOwner { … private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
  31. public class MyLifecycleOwnerActivity extends Activity implements LifecycleOwner { private LifecycleRegistry

    lifecycleRegistry; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); lifecycleRegistry = new LifecycleRegistry(this); lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); } @Override protected void onStart() { super.onStart(); lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START); } … @NonNull @Override public Lifecycle getLifecycle() { return lifecycleRegistry; } … ϥΠϑαΠΫϧϝιουͰ handleLifecycleEvent() ΛݺͿ
  32. onSaveInstanceState onSaveInstanceState() onStart() UI onSaveInstanceState() onStop() FragmentActivity v4 support library

    Fragment onSaveInstanceState() CREATED
  33. Lifecycle Extensions

  34. Lifecycle extensions Ϟδϡʔϧͷ build.gradle dependencies { implementation "android.arch.lifecycle:extensions:1.0.0" } *

    LiveData, ViewModel ΋ extensions ʹؚ·Ε͍ͯ·͕͢ࠓճ͸औΓ্͛·ͤΜ https://developer.android.com/topic/libraries/architecture/adding-components.html
  35. LifecycleService LifecycleOwner Service public class MyService extends LifecycleService { @Override

    public void onCreate() { super.onCreate(); final Lifecycle lifecycle = getLifecycle(); } }
  36. ServiceLifecycleDispatcher Service Lifecycle LifecycleService LifecycleService public class LifecycleService extends Service

    implements LifecycleOwner { private final ServiceLifecycleDispatcher mDispatcher = new ServiceLifecycleDispatcher(this); @CallSuper @Override public void onCreate() { mDispatcher.onServicePreSuperOnCreate(); super.onCreate(); }
  37. ProcessLifecycleOwner Lifecycle ON_CREATE ON_DESTROY ProcessLifecycleOwner.get() .getLifecycle() .addObserver(observer);

  38. ProcessLifecycleOwner ON_START, ON_RESUME Activity ON_STOP, ON_PAUSE Activity * ஗Ԇ͸ destruction

    ࣌ͷΈ
  39. None
  40. 0 appcompat-v7:26.1.0

  41. 1 Lifecycle current state Activity onCreate(), onStart(), onResume(), onPause(), onStop(),

    onDestroy(), onRestart(), onSaveInstanceState()) onCreate() 1 onSaveInstanceState() super.onSaveInstanceState()
  42. @Override protected void onResume() { super.onResume(); Log.d("Lifecycle", "onResume : "

    + getLifecycle().getCurrentState().name()); } …
  43. onCreate() onStart() onResume() onPause() onStop() onDestroy() onRestart() onCreate() 1 onSaveInstanceState()

  44. None
  45. 2 LifecycleObserver Lifecycle.Event ON_CREATE, ON_START, ON_RESUME, ON_PAUSE, ON_STOP, ON_DESTROY, ON_ANY

    Lifecycle LifecycleObserver
  46. final LifecycleObserver observer = new LifecycleObserver() { @OnLifecycleEvent(Lifecycle.Event.ON_START) public void

    calledWhenOnStart(LifecycleOwner source) { Log.d("Lifecycle", "ON_START : " + source.getLifecycle().getCurrentState().name()); } @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void calledWhenOnAny(LifecycleOwner source, Lifecycle.Event event) { Log.d("Lifecycle", "ON_ANY : " + source.getLifecycle().getCurrentState().name()); } … }; lifecycle.addObserver(observer);
  47. 2 2 Observer addObserver LifecycleObserver A : onDestory() removeObserver() B

    : ON_DESTROY LifecycleOwner Lifecycle removeObserver()
  48. 3 GenericLifecycleObserver onStateChanged() Lifecycle GenericLifecycleObserver GenericLifecycleObserver @hide @OnLifecycleEvent(Lifecycle.Event.ON_ANY)

  49. 4 Application onCreate() ProcessLifecycleOwner Lifecycle GenericLifecycleObserver Recent Apps Activity

  50. final LifecycleObserver observer = new GenericLifecycleObserver() { @Override public void

    onStateChanged(LifecycleOwner source, Lifecycle.Event event) { Log.d("Lifecycle", "onStateChanged : " + source + ", " + event.name()); } }; ProcessLifecycleOwner.get() .getLifecycle() .addObserver(observer);
  51. Lifecycle.Event Recent Apps Activity Activity

  52. RecentApps ProcessLifecycleOwner ON_CREATE Recent Apps ProcessLifecycleOwner ON_CREATE

  53. 5 RxJava2 AsyncTask STARTED UI

  54. 6 JobIntentService JobIntentService foreground Activity Toast https://developer.android.com/reference/android/support/v4/app/ JobIntentService.html

  55. public class SimpleJobIntentService extends JobIntentService { private static final int

    JOB_ID = 1000; static void enqueueWork(@NonNull Context context, @NonNull Intent work) { enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work); } @Override protected void onHandleWork(@NonNull Intent intent) { … } @Override public void onDestroy() { super.onDestroy(); // TODO: ΞϓϦ͕ foreground ͳΒμΠΞϩάςʔϚͷ Activity ʹભҠ͠ɺ // TODO: όοΫάϥ΢ϯυͳΒ Toast Λग़͢Α͏ʹ௚͢ Toast.makeText(this, "All work complete", Toast.LENGTH_SHORT).show(); } }
  56. <uses-permission android:name="android.permission.WAKE_LOCK" /> <service android:name=".SimpleJobIntentService" android:permission="android.permission.BIND_JOB_SERVICE"/> <activity android:name=".DialogActivity" android:theme="@style/Theme.AppCompat.Light.Dialog" />

  57. 7 register, unregister, ON_RESUME register ON_PAUSE unregister ON_DESTROY Lifecycle Activity

    onCreate() Lifecycle
  58. public class SensorActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable

    Bundle savedInstanceState) { super.onCreate(savedInstanceState); final SensorHandler sensorHandler = … getLifecycle().addObserver(sensorHandler); } public static class SensorHandler implements SensorEventListener, LifecycleObserver { private final SensorManager mSensorManager; private final Sensor mAccelerometer; SensorHandler(SensorManager sensorManager) { mSensorManager = sensorManager; mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } … } }