/** * Adapted from http://pivotal.github.com/robolectric/roboguice.html */ @RoboGuice(Counter.class) @EActivity(R.layout.injected) public class ActivityWithRoboGuice extends Activity { @InjectResource(R.string.injected_activity_caption) String caption; @InjectView(R.id.injected_text_view) TextView injectedTextView; @Inject Date date; @AfterViews void updateCaption() { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.US); String formattedDate = dateFormat.format(date); injectedTextView.setText(caption + " - " + formattedDate); } }
public class CustomClass { @Inject Other other; Foo foo; public CustomClass(Context context) { final RoboInjector injector = RoboGuice.getInjector(context); // This will inject all fields marked with the @Inject annotation injector.injectMembersWithoutViews(this); // This will create an instance of Foo foo = injector.getInstance(Foo.class); } }
public static boolean notNullable( Field field ) { return !isNullable( field ); }
@Override public void setPreferenceScreen(PreferenceScreen preferenceScreen) { super.setPreferenceScreen(preferenceScreen); final ContextScope scope = RoboGuice.getInjector(this).getInstance(ContextScope.class); synchronized (ContextScope.class) { scope.enter(this); try { preferenceListener.injectPreferenceViews(); } finally { scope.exit(this); } } }
final RoboInjector injector = RoboGuice.getInjector(this); injector.injectMembersWithoutViews(this);
@ContextSingleton public class FragmentManagerProvider implements Provider<FragmentManager> { @Inject protected Activity activity; @Override public FragmentManager get() { // BUG only supports compat library at the moment. Does not support honeycomb directly yet return activity.getFragmentManager(); } }
@Override public void setPreferenceScreen(PreferenceScreen preferenceScreen) { super.setPreferenceScreen(preferenceScreen); final ContextScope scope = RoboGuice.getInjector(this).getInstance(ContextScope.class); synchronized (ContextScope.class) { scope.enter(this); try { preferenceListener.injectPreferenceViews(); } finally { scope.exit(this); } } }
RoboInjector injector = RoboGuice.getInjector(context); injector.injectMembersWithoutViews(yourObjectB);
@Override public <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> typeLiteral) { //System.out.println("in getMembersInjector"); synchronized (ContextScope.class) { scope.enter(context, scopedObjects); try { return delegate.getMembersInjector(typeLiteral); } finally { scope.exit(context); } } }