Bundle Injection with Dagger

Bundle Injection with Dagger

See blog post: “Bundle Injection with Dagger” https://medium.com/@oldergod/bundle-injection-with-dagger-fe98b59f1cc6

05162bc961c3654218bf1839974a4f35?s=128

Benoît Quenaudon

September 18, 2017
Tweet

Transcript

  1. Bundle injection with Dagger Benoît Quenaudon @oldergod

  2. public class InputActivity extends Activity { @Override public void onCreate(@Nullable

    Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); final EditText editText = findViewById(R.id.edit_text); }a }b
  3. public class InputActivity extends Activity { @Override public void onCreate(@Nullable

    Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); final EditText editText = findViewById(R.id.edit_text); findViewById(R.id.button).setOnClickListener(v -> { }); }a }b
  4. public class InputActivity extends Activity { @Override public void onCreate(@Nullable

    Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); final EditText editText = findViewById(R.id.edit_text); findViewById(R.id.button).setOnClickListener(v -> { Intent intent = new Intent(InputActivity.this, OutputActivity.class); intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString()); }); }a }b
  5. public class InputActivity extends Activity { @Override public void onCreate(@Nullable

    Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); final EditText editText = findViewById(R.id.edit_text); findViewById(R.id.button).setOnClickListener(v -> { Intent intent = new Intent(InputActivity.this, OutputActivity.class); intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString()); InputActivity.this.startActivity(intent); }); }a }b
  6. public class InputActivity extends Activity { @Override public void onCreate(@Nullable

    Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); final EditText editText = findViewById(R.id.edit_text); findViewById(R.id.button).setOnClickListener(v -> { Intent intent = new Intent(InputActivity.this, OutputActivity.class); intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString()); InputActivity.this.startActivity(intent); }); }a }b
  7. public class OutputActivity extends Activity { OutputPresenter presenter; @Override public

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); }a }c
  8. public class OutputActivity extends Activity { OutputPresenter presenter; @Override public

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); presenter = new OutputPresenter(inputText); }a }c
  9. public class OutputActivity extends Activity { @Inject OutputPresenter presenter; @Override

    public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); }a }c
  10. public class OutputActivity extends Activity { @Inject OutputPresenter presenter; @Override

    public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); presenter.setInputText(inputText); }a }c
  11. public class OutputActivity extends Activity { @Inject OutputPresenter presenter; @Override

    public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); presenter.setInputText(inputText); }a @Override protected void onStart() { super.onStart(); presenter.onStart(); } }c
  12. None
  13. public class BundleService { private final Bundle data; public BundleService()

    { data = new Bundle(); }c public Object get(String key) { return data.get(key); }d public Bundle getAll() { return data; }f }e
  14. public class BundleService { private final Bundle data; public BundleService(@Nullable

    Bundle savedState) { data = new Bundle(); if (savedState != null) { data.putAll(savedState); }a }c public Object get(String key) { return data.get(key); }d public Bundle getAll() { return data; }f }e
  15. public class BundleService { private final Bundle data; public BundleService(@Nullable

    Bundle savedState, @Nullable Bundle intentExtras) { data = new Bundle(); if (savedState != null) { data.putAll(savedState); }a if (intentExtras != null) { data.putAll(intentExtras); }b }c public Object get(String key) { return data.get(key); }d public Bundle getAll() { return data; }f }e
  16. public class BundleService { private final Bundle data; public BundleService(@Nullable

    Bundle savedState, @Nullable Bundle intentExtras) { data = new Bundle(); if (savedState != null) { data.putAll(savedState); }a if (intentExtras != null) { data.putAll(intentExtras); }b }c public Object get(String key) { return data.get(key); }d public Bundle getAll() { return data; }f }e
  17. public abstract class BaseActivity extends Activity { private BundleService bundleService;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) { bundleService = new BundleService(savedInstanceState, getIntent().getExtras()); super.onCreate(savedInstanceState); }a }d
  18. public abstract class BaseActivity extends Activity { private BundleService bundleService;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) { bundleService = new BundleService(savedInstanceState, getIntent().getExtras()); super.onCreate(savedInstanceState); }a public BundleService getBundleService() { return bundleService; }b }d
  19. public abstract class BaseActivity extends Activity { private BundleService bundleService;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) { bundleService = new BundleService(savedInstanceState, getIntent().getExtras()); super.onCreate(savedInstanceState); }a public BundleService getBundleService() { return bundleService; }b @Override protected void onSaveInstanceState(Bundle outState) { outState.putAll(bundleService.getAll()); super.onSaveInstanceState(outState); }c }d
  20. public abstract class BaseActivity extends Activity { private BundleService bundleService;

    @Override public void onCreate(@Nullable Bundle savedInstanceState) { bundleService = new BundleService(savedInstanceState, getIntent().getExtras()); super.onCreate(savedInstanceState); }a public BundleService getBundleService() { return bundleService; }b @Override protected void onSaveInstanceState(Bundle outState) { outState.putAll(bundleService.getAll()); super.onSaveInstanceState(outState); }c }d
  21. @Module public abstract class BundleModule { @Provides static BundleService provideBundleService(BaseActivity

    activity) { return activity.getBundleService(); } }
  22. None
  23. @Qualifier @Retention(RUNTIME) public @interface SomeInput {}a

  24. @Qualifier @Retention(RUNTIME) public @interface SomeInput {}a @Module public abstract class

    BundleModule { public static final String EXTRA_SOME_INPUT = "SOME_INPUT"; @Provides static BundleService provideBundleService(BaseActivity activity) { return activity.getBundleService(); } @Provides @SomeInput static String provideSomeInput(BundleService bundleService) { String style = (String) bundleService.get(EXTRA_SOME_INPUT); return style == null ? "Null" : style; } }
  25. class OutputPresenter { private final String inputText; OutputPresenter(String inputText) {

    this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  26. class OutputPresenter { private final String inputText; @Inject OutputPresenter(String inputText)

    { this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  27. class OutputPresenter { private final String inputText; @Inject OutputPresenter(@SomeInput String

    inputText) { this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  28. class OutputPresenter { private final String inputText; @Inject OutputPresenter(@SomeInput String

    inputText) { this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  29. public class OutputActivity extends BaseActivity { OutputPresenter presenter; @Override public

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_output); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); presenter = new OutputPresenter(inputText); }a @Override protected void onStart() { super.onStart(); presenter.onStart(); }b }c
  30. public class OutputActivity extends BaseActivity { OutputPresenter presenter; @Override public

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_output); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); presenter = new OutputPresenter(inputText); }a @Override protected void onStart() { super.onStart(); presenter.onStart(); }b }c
  31. public class OutputActivity extends BaseActivity { @Inject OutputPresenter presenter; @Override

    public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_output); String inputText = getIntent().getStringExtra(EXTRA_SOME_INPUT); presenter = new OutputPresenter(inputText); }a @Override protected void onStart() { super.onStart(); presenter.onStart(); }b }c
  32. public class OutputActivity extends BaseActivity { @Inject OutputPresenter presenter; @Override

    public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_output); }a @Override protected void onStart() { super.onStart(); presenter.onStart(); }b }c
  33. public class OutputActivity extends BaseActivity { @Inject OutputPresenter presenter; @Override

    public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_output); }a @Override protected void onStart() { super.onStart(); presenter.onStart(); }b }c
  34. Fin Benoît Quenaudon @oldergod