Created
November 22, 2016 21:59
-
-
Save hannesstruss/cef55e6d4dd0153e27f72a19a9588f8b to your computer and use it in GitHub Desktop.
Conductor Injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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