Created
September 15, 2014 15:35
-
-
Save fedefernandez/23d3ced232459f29f428 to your computer and use it in GitHub Desktop.
Abstract ActionBar with Dagger (from Architecting Android Applications with Dagger - Jake Wharton)
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 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 | |
} | |
} |
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
@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(); | |
} | |
} |
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 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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