Skip to content

Instantly share code, notes, and snippets.

@amlcurran
Created May 21, 2014 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amlcurran/2ddd18ec83844bbe7f71 to your computer and use it in GitHub Desktop.
Save amlcurran/2ddd18ec83844bbe7f71 to your computer and use it in GitHub Desktop.
Injection of listeners through Activty
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
messageLoader = new ProviderHelper<MessagesLoaderProvider>(MessagesLoaderProvider.class).get(activity).getMessagesLoader();
}
package com.amlcurran.messages;
public class ProviderHelper<T> {
private Class<T> requiredClass;
public ProviderHelper(Class<T> requiredClass) {
this.requiredClass = requiredClass;
}
public T get(Object implementer) {
try {
return requiredClass.cast(implementer);
} catch (ClassCastException cce) {
String detailMessage = String.format("%1$s doesn't implement the required interface %2$s",
implementer.getClass().getSimpleName(), requiredClass.getSimpleName());
throw new ClassCastException(detailMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment