Skip to content

Instantly share code, notes, and snippets.

@namphho
Last active March 20, 2019 04:20
Show Gist options
  • Save namphho/ea68f0d48614136b96de888e8d426096 to your computer and use it in GitHub Desktop.
Save namphho/ea68f0d48614136b96de888e8d426096 to your computer and use it in GitHub Desktop.
/** Injects core Android types. */
@Beta
public final class AndroidInjection {
private static final String TAG = "dagger.android";
/**
* 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);
}
....
private AndroidInjection() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment