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

Bundle Injection with Dagger

Bundle Injection with Dagger

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

Benoît Quenaudon

September 18, 2017
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. 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
  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); findViewById(R.id.button).setOnClickListener(v -> { }); }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 -> { Intent intent = new Intent(InputActivity.this, OutputActivity.class); intent.putExtra(EXTRA_SOME_INPUT, editText.getText().toString()); }); }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()); InputActivity.this.startActivity(intent); }); }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 OutputActivity extends Activity { OutputPresenter presenter; @Override public

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); }a }c
  7. 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
  8. 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
  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); presenter.setInputText(inputText); }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 @Override protected void onStart() { super.onStart(); presenter.onStart(); } }c
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  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 public BundleService getBundleService() { return bundleService; }b @Override protected void onSaveInstanceState(Bundle outState) { outState.putAll(bundleService.getAll()); super.onSaveInstanceState(outState); }c }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 @Override protected void onSaveInstanceState(Bundle outState) { outState.putAll(bundleService.getAll()); super.onSaveInstanceState(outState); }c }d
  19. @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; } }
  20. class OutputPresenter { private final String inputText; OutputPresenter(String inputText) {

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

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

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

    inputText) { this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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