Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save parallelcross/f9571d89e9c49d95bde1 to your computer and use it in GitHub Desktop.
Save parallelcross/f9571d89e9c49d95bde1 to your computer and use it in GitHub Desktop.
I used to always call a setListener method on my fragment after it was instantiated, but recently this is how I've started attaching my fragment's listener. I like it because it doesn't require the client of the fragment to remember to set the listener, and throws a runtime exception if they don't implement the listener.
private CategoryFragmentListener mListener = sCategoryListener;
private static CategoryFragmentListener sCategoryListener = new CategoryFragmentListener() {
@Override
public void onLeafCategoryChosen() {
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mListener = (CategoryFragmentListener) activity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment