Revisions
-
mlc revised this gist
May 16, 2012 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,12 +4,13 @@ public abstract class ContractFragment<T> extends Fragment { @Override public void onAttach(Activity activity) { if (!((Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]).isAssignableFrom(activity.getClass())) throw new IllegalStateException(activity.getClass().getSimpleName() + " does not implement " + getClass().getSimpleName() + "'s contract interface."); //noinspection unchecked contract = (T)activity; super.onAttach(activity); } -
JakeWharton revised this gist
May 7, 2012 . 1 changed file with 29 additions and 27 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,34 +1,36 @@ /* Base fragment to ensure the parent activity implements a contract interface. */ public abstract class ContractFragment<T> extends Fragment { private T mContract; @Override public void onAttach(Activity activity) { try { mContract = (T)activity; } catch (ClassCastException e) { throw new IllegalStateException(activity.getClass().getSimpleName() + " does not implement " + getClass().getSimpleName() + "'s contract interface.", e); } super.onAttach(activity); } @Override public void onDetach() { super.onDetach(); mContract = null; } public final T getContract() { return mContract; } } /* Example fragment showing usage of ContractFragment. */ public class MyCoolFragment extends ContractFragment<MyCoolFragment.Contract> { public interface Contract { void sayHi(String message); } public void somethingHappened() { getContract().sayHi("Hi, Mom!"); } } -
JakeWharton created this gist
May 6, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ /* Base fragment to ensure the parent activity implements a contract interface. */ public abstract class ContractFragment<T> extends Fragment { private T mContract; @Override public void onAttach(Activity activity) { try { mContract = (T)activity; } catch (ClassCastException e) { throw new IllegalStateException(activity.getClass().getSimpleName() + " does not implement " + getClass().getSimpleName() + "'s contract interface.", e); } } @Override public void onDetach() { mContract = null; } public final T getContract() { return mContract; } } /* Example fragment showing usage of ContractFragment. */ public class MyCoolFragment extends ContractFragment<Contract> { public interface Contract { void sayHi(String message); } public void somethingHappened() { getContract().sayHi("Hi, Mom!"); } }