Skip to content

Instantly share code, notes, and snippets.

@jacquesgiraudel
Created March 16, 2016 06:26
Show Gist options
  • Save jacquesgiraudel/e4983cc503fed4f3045b to your computer and use it in GitHub Desktop.
Save jacquesgiraudel/e4983cc503fed4f3045b to your computer and use it in GitHub Desktop.
fragmentlistener-snippet
// 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