Skip to content

Instantly share code, notes, and snippets.

@fedefernandez
Created September 15, 2014 15:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fedefernandez/23d3ced232459f29f428 to your computer and use it in GitHub Desktop.
Save fedefernandez/23d3ced232459f29f428 to your computer and use it in GitHub Desktop.
Abstract ActionBar with Dagger (from Architecting Android Applications with Dagger - Jake Wharton)
public class SignUpActivity extends Activity {
private final ObjectGraph childOg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState):
ExampleApp app = (ExampleApp) getApplication();
ObjectGraph og = app.getObjectGraph();
childOg = og.plus(new SignUpModule(this));
// Content set up and injection
}
}
@Module (
injects = {
SignUpView.class, ForgotPasswordView.class
}
)
public class SignUpModule {
private final SignUpActivity signUpActivity;
public SignUpModule(SignUpActivity signUpActivity) {
this.signUpActivity = signUpActivity;
}
@Provides
@Singleton
ActionBar provideActionBar() {
return signUpActivity.getActionBar();
}
}
public class SignUpView extends LinearLayout {
@Inject ActionBar actionBar;
public SignUpView(Context context, AttributeSet attrs) {
super(context, attrs);
// Note: Injection happens by parent activity
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
actionBar.setTitle(R.string.sign_up);
}
}
@Sloy
Copy link

Sloy commented Jun 5, 2015

Buscando algún ejemplito por Google me encuentro con esto. Y al rato de estar ojeándolo me veo que es tuyo Fede :p
Me ha venido bien para refrescar, gracias! jeje

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment