Skip to content

Instantly share code, notes, and snippets.

@davespanton
Created May 3, 2012 20:47
Show Gist options
  • Save davespanton/2589240 to your computer and use it in GitHub Desktop.
Save davespanton/2589240 to your computer and use it in GitHub Desktop.
Assisted Injection
// the factory interface
public interface SMSSendingAlarmFactory {
public SMSSendingAlarm create(Context context);
}
// in the module class
protected void configure() {
// ... various other bindings ...
install(new FactoryModuleBuilder()
.build(SMSSendingAlarmFactory.class));
}
// the assisted injection constructor
@AssistedInject
public SMSSendingAlarm(@Assisted Context context) {
this.context = context;
}
// in use elsewhere in the application...
private SMSSendingAlarm smsAlarm;
@Inject
private SMSSendingAlarmFactory smsAlarmFactory;
@Override
public void onCreate() {
super.onCreate();
smsAlarm = smsAlarmFactory.create(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment