Created
March 16, 2016 06:26
-
-
Save jacquesgiraudel/e4983cc503fed4f3045b to your computer and use it in GitHub Desktop.
fragmentlistener-snippet
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
// Class consuming the interaction | |
public class ExempleActivity extends Activity implements ExempleFragment.OnFragmentInteractionListener { | |
... | |
@Override | |
public void onFragmentInteraction(Uri uri) { | |
// Consumption of the interaction | |
} | |
} | |
// Class producing the interaction | |
public class ExempleFragment extends Fragment { | |
private OnFragmentInteractionListener mListener; | |
... | |
public void onButtonPressed(Uri uri) { | |
if (mListener != null) { | |
// Production of the interaction | |
mListener.onFragmentInteraction(uri); | |
} | |
} | |
// Attachment of the listener | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
if (context instanceof OnFragmentInteractionListener) { | |
mListener = (OnFragmentInteractionListener) context; | |
} else { | |
throw new RuntimeException(context.toString() | |
+ " must implement OnFragmentInteractionListener"); | |
} | |
} | |
// Detachment of the listener | |
@Override | |
public void onDetach() { | |
super.onDetach(); | |
mListener = null; | |
} | |
// Declaration of the interaction method | |
public interface OnFragmentInteractionListener { | |
void onFragmentInteraction(Uri uri); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment