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. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 7.

    public class OutputActivity extends Activity { OutputPresenter presenter; @Override public

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); }a }c
  7. 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
  8. 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
  9. 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
  10. 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
  11. 12.
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 22.
  21. 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; } }
  22. 25.

    class OutputPresenter { private final String inputText; OutputPresenter(String inputText) {

    this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  23. 26.

    class OutputPresenter { private final String inputText; @Inject OutputPresenter(String inputText)

    { this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  24. 27.

    class OutputPresenter { private final String inputText; @Inject OutputPresenter(@SomeInput String

    inputText) { this.inputText = inputText; }a void onStart() { repository.doSomeWork(inputText); }b }c
  25. 28.

    class OutputPresenter { private final String inputText; @Inject OutputPresenter(@SomeInput String

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