Created
May 21, 2014 15:33
-
-
Save amlcurran/2ddd18ec83844bbe7f71 to your computer and use it in GitHub Desktop.
Injection of listeners through Activty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
messageLoader = new ProviderHelper<MessagesLoaderProvider>(MessagesLoaderProvider.class).get(activity).getMessagesLoader(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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