@Override public void onCreate() { super.onCreate(); DaggerAppComponent.builder().create(this).inject(this); }
/** * Lazily injects the {@link DaggerApplication}'s members. Injection cannot be performed in {@link * Application#onCreate()} since {@link android.content.ContentProvider}s' {@link * android.content.ContentProvider#onCreate() onCreate()} method will be called first and might * need injected members on the application. Injection is not performed in the constructor, as * that may result in members-injection methods being called before the constructor has completed, * allowing for a partially-constructed instance to escape. */ private void injectIfNecessary() { if (needToInject) { synchronized (this) { if (needToInject) { @SuppressWarnings("unchecked") AndroidInjector<DaggerApplication> applicationInjector = (AndroidInjector<DaggerApplication>) applicationInjector(); applicationInjector.inject(this); if (needToInject) { throw new IllegalStateException( "The AndroidInjector returned from applicationInjector() did not inject the " + "DaggerApplication"); } } } } }
/** * Injects {@code service} if an associated {@link AndroidInjector} implementation can be found, * otherwise throws an {@link IllegalArgumentException}. * * @throws RuntimeException if the {@link Application} doesn't implement {@link * HasServiceInjector}. */ public static void inject(Service service) { checkNotNull(service, "service"); Application application = service.getApplication(); if (!(application instanceof HasServiceInjector)) { throw new RuntimeException( String.format( "%s does not implement %s", application.getClass().getCanonicalName(), HasServiceInjector.class.getCanonicalName())); } AndroidInjector<Service> serviceInjector = ((HasServiceInjector) application).serviceInjector(); checkNotNull(serviceInjector, "%s.serviceInjector() returned null", application.getClass()); serviceInjector.inject(service); }
/** * Injects {@code activity} if an associated {@link AndroidInjector} implementation can be found, * otherwise throws an {@link IllegalArgumentException}. * * @throws RuntimeException if the {@link Application} doesn't implement {@link * HasActivityInjector}. */ public static void inject(Activity activity) { checkNotNull(activity, "activity"); Application application = activity.getApplication(); if (!(application instanceof HasActivityInjector)) { throw new RuntimeException( String.format( "%s does not implement %s", application.getClass().getCanonicalName(), HasActivityInjector.class.getCanonicalName())); } AndroidInjector<Activity> activityInjector = ((HasActivityInjector) application).activityInjector(); checkNotNull(activityInjector, "%s.activityInjector() returned null", application.getClass()); activityInjector.inject(activity); }
/** * Attempts to perform members-injection on {@code instance}, returning {@code true} if * successful, {@code false} otherwise. * * @throws InvalidInjectorBindingException if the injector factory bound for a class does not * inject instances of that class */ @CanIgnoreReturnValue public boolean maybeInject(T instance) { Provider<AndroidInjector.Factory<?>> factoryProvider = injectorFactories.get(instance.getClass().getName()); if (factoryProvider == null) { return false; } @SuppressWarnings("unchecked") AndroidInjector.Factory<T> factory = (AndroidInjector.Factory<T>) factoryProvider.get(); try { AndroidInjector<T> injector = checkNotNull( factory.create(instance), "%s.create(I) should not return null.", factory.getClass()); injector.inject(instance); return true; } catch (ClassCastException e) { throw new InvalidInjectorBindingException( String.format( "%s does not implement AndroidInjector.Factory<%s>", factory.getClass().getCanonicalName(), instance.getClass().getCanonicalName()), e); } }
/** * Injects {@code contentProvider} if an associated {@link AndroidInjector} implementation can be * found, otherwise throws an {@link IllegalArgumentException}. * * @throws RuntimeException if the {@link Application} doesn't implement {@link * HasContentProviderInjector}. */ public static void inject(ContentProvider contentProvider) { checkNotNull(contentProvider, "contentProvider"); Application application = (Application) contentProvider.getContext().getApplicationContext(); if (!(application instanceof HasContentProviderInjector)) { throw new RuntimeException( String.format( "%s does not implement %s", application.getClass().getCanonicalName(), HasContentProviderInjector.class.getCanonicalName())); } AndroidInjector<ContentProvider> contentProviderInjector = ((HasContentProviderInjector) application).contentProviderInjector(); checkNotNull( contentProviderInjector, "%s.contentProviderInjector() returned null", application.getClass()); contentProviderInjector.inject(contentProvider); }
/** * Injects {@code broadcastReceiver} if an associated {@link AndroidInjector} implementation can * be found, otherwise throws an {@link IllegalArgumentException}. * * @throws RuntimeException if the {@link Application} from {@link * Context#getApplicationContext()} doesn't implement {@link HasBroadcastReceiverInjector}. */ public static void inject(BroadcastReceiver broadcastReceiver, Context context) { checkNotNull(broadcastReceiver, "broadcastReceiver"); checkNotNull(context, "context"); Application application = (Application) context.getApplicationContext(); if (!(application instanceof HasBroadcastReceiverInjector)) { throw new RuntimeException( String.format( "%s does not implement %s", application.getClass().getCanonicalName(), HasBroadcastReceiverInjector.class.getCanonicalName())); } AndroidInjector<BroadcastReceiver> broadcastReceiverInjector = ((HasBroadcastReceiverInjector) application).broadcastReceiverInjector(); checkNotNull( broadcastReceiverInjector, "%s.broadcastReceiverInjector() returned null", application.getClass()); broadcastReceiverInjector.inject(broadcastReceiver); }
fragmentInjector, "%s.fragmentInjector() returned null", hasFragmentInjector.getClass()); fragmentInjector.inject(fragment);
hasSupportFragmentInjector.getClass()); fragmentInjector.inject(fragment);
@Override protected void onCreate(Bundle savedInstanceState) { //AndroidInjection.inject(this); ReadhubApplicationLike.getInstance().activityInjector() .inject(this); super.onCreate(savedInstanceState); if (null != mPresenter) { mPresenter.onAttach(this); } }
@Override protected void onCreate(Bundle savedInstanceState) { //MyAndroidInjection.inject(this); ReadhubApplicationLike.getInstance().activityInjector().inject(this); super.onCreate(savedInstanceState); getSwipeBackLayout().setEdgeOrientation(SwipeBackLayout.EDGE_ALL); if (null != mPresenter) { mPresenter.onAttach(this); } }