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

Architecture Components 勉強会 第1回目 Lifecycles

Yuki Anzai
December 20, 2017

Architecture Components 勉強会 第1回目 Lifecycles

Yuki Anzai

December 20, 2017
Tweet

More Decks by Yuki Anzai

Other Decks in Technology

Transcript

  1. 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
  2. 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
  3. 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
  4. 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 ʹͳ͍ͬͯΔ
  5. LifecycleOwner Lifecycle single method interface FragmentActivity LifecycleOwner v4 support library

    Fragment LifecycleOwner public interface LifecycleOwner { @NonNull Lifecycle getLifecycle(); }
  6. 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 Λ ࢖࣮ͬͯ૷͞Ε͍ͯΔ
  7. FragmentActivity Lifecycle public class MainActivity extends AppCompatActivity { @Override protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Lifecycle lifecycle = getLifecycle(); }
  8. 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() { } };
  9. 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"); } };
  10. OnLifecycleEvent 1 LifecycleOwner final LifecycleObserver observer = new LifecycleObserver() {

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

    { @OnLifecycleEvent(Lifecycle.Event.ON_ANY) public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { } };
  12. 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)
  13. 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);
  14. 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() ΛݺͿ
  15. 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
  16. LifecycleService LifecycleOwner Service public class MyService extends LifecycleService { @Override

    public void onCreate() { super.onCreate(); final Lifecycle lifecycle = getLifecycle(); } }
  17. 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(); }
  18. 1 Lifecycle current state Activity onCreate(), onStart(), onResume(), onPause(), onStop(),

    onDestroy(), onRestart(), onSaveInstanceState()) onCreate() 1 onSaveInstanceState() super.onSaveInstanceState()
  19. 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);
  20. 2 2 Observer addObserver LifecycleObserver A : onDestory() removeObserver() B

    : ON_DESTROY LifecycleOwner Lifecycle removeObserver()
  21. 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);
  22. 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(); } }
  23. 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); } … } }