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

Dagger2 라이트하게 훑기

Dagger2 라이트하게 훑기

2018년 2월 2일 GDG 안드로이드 라이트닝 토크에서 발표한 자료입니다. Dagger2를 배우기 위한 기본 이론부터 Dagger2 개념, 사용방법까지 라이트하게 다루었습니다.

Seungmin 마량

February 02, 2018
Tweet

More Decks by Seungmin 마량

Other Decks in Technology

Transcript

  1. ੄ઓࢿ ઱ੑ਷ ޖ঺ੌө? A classо B classܳ ੄ઓೡ ٸ B

    Object ࢤࢿਸ Aীࢲ ೞ૑ ঋҊ ৻ࠗীࢲ ࢤࢿೞৈ ֈѹ઱ח Ѫ
  2. Daggerо ੄ઓࢿਸ ઱ੑೞח Flow @Inject Scopeী ੓ਵݶ return হਵݶ ࢤࢿ

    Subcomponent Module Scopeী ੓ਵݶ return হਵݶ ࢤࢿ Module Subcomponent Moduleীࢲ ݏח ఋੑਸ ޅ଺ਵݶ ࢚ਤ Component
  3. @Singleton @Component(modules = {AndroidSupportInjectionModule.class, ActivityBindingModule.class, ApplicationModule.class}) public interface AppComponent extends

    AndroidInjector<BaseApplication> { @Component.Builder interface Builder { @BindsInstance AppComponent.Builder application(Application application); AppComponent build(); } } Application Component
  4. @Module public class ApplicationModule { @Provides Context providesContext(Application application){ return

    application; } } Context ఋੑ੄ ੄ઓࢿ ё୓ܳ ࢤࢿೠ׮ Application Module
  5. public class BaseApplication extends DaggerApplication { @Override protected AndroidInjector<? extends

    DaggerApplication> applicationInjector() { return DaggerAppComponent.builder().application(this).build(); } } BaseApplication Component.inject() ೧ષ
  6. @Module public abstract class ActivityBindingModule { @ActivityScoped @ContributesAndroidInjector(modules = TasksModule.class)

    abstract TasksActivity tasksActivity(); @ActivityScoped @ContributesAndroidInjector(modules = TaskDetailPresenterModule.class) abstract TaskDetailActivity taskDetailActivity(); } ActivitySubcomponent ࢤࢿ
  7. @Module public abstract class TasksModule { @FragmentScoped @ContributesAndroidInjector abstract TasksFragment

    tasksFragment(); @ActivityScoped @Provides static TasksPresenter taskPresenter(){ return new TasksPresenter(); } } ActivitySubcomponent੄ Module TasksPresenter ఋੑ੄ ੄ઓࢿ ё୓ܳ ࢤࢿೠ׮
  8. public class TasksActivity extends DaggerAppCompatActivity { @Inject TasksPresenter mTasksPresenter; @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tasks_act); } } TasksActivity Component.inject() ೧ષ ੄ઓࢿ ઱ੑ ਃ୒
  9. @Module public abstract class TasksModule { @FragmentScoped @ContributesAndroidInjector abstract TasksFragment

    tasksFragment(); @ActivityScoped @Provides static TasksPresenter taskPresenter(){ return new TasksPresenter(); } } FragmentSubcomponent ࢤࢿ
  10. @ActivityScoped public class TasksFragment extends DaggerFragment { @Inject TasksContract.Presenter mPresenter;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mListAdapter = new TasksAdapter(new ArrayList<Task>(0), mItemListener); } } TasksFragment Component.inject() ೧ષ ੄ઓࢿ ઱ੑ ਃ୒
  11. Inject ࢤࢿ੗ @Injectо ੄ઓࢿ ਃ୒ ৻ী ё୓ࢤࢿীب ࢎਊػ׮. పझ౟ పझ౟

    ё୓, पઁ ё୓ܳ औѱ աׂ ࣻ ੓׮. য়ט ޅ׮ ࠁৈ٘ܽ Dagger Dagger2 ҕध ࢎ੉౟: https://google.github.io/dagger/ Dagger2ܳ ࢎਊೠ Google੄ Architecture Sample: https://github.com/googlesamples/android-architecture/tree/todo-mvp-dagger/