Skip to content

Instantly share code, notes, and snippets.

@hannesstruss
Created November 22, 2016 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannesstruss/cef55e6d4dd0153e27f72a19a9588f8b to your computer and use it in GitHub Desktop.
Save hannesstruss/cef55e6d4dd0153e27f72a19a9588f8b to your computer and use it in GitHub Desktop.
Conductor Injection
class BaseActivity extends AppCompatActivity {
private ActivityComponent activityComponent;
@Override public void onCreate(Bundle savedInstanceState) {
activityComponent = DaggerActivityComponent.builder()
.appComponent(((MyApp) getApplicationContext()).getComponent())
.build();
activityComponent.inject(this);
}
public ActivityComponent getComponent() {
return activityComponent;
}
}
public abstract class BaseController extends Controller {
private boolean injected = false;
@NonNull @Override
final protected View onCreateView(LayoutInflater inflater, ViewGroup container) {
inject(inflater.getContext());
//...create view...
}
private void inject(Context context) {
if (!created) {
injected = true;
onInject((BaseActivity) context).getComponent());
}
}
protected abstract void onInject(ActivityComponent component);
}
class SomeController extends BaseController {
protected void onInject(ActivityComponent component) {
component.inject(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment