Droid Dev Stream
P145: Fragments p5: Communicating events

In your Fragment class, if you create listener on a particular interface, and make the parent implement that interface, you can communicate via events.

First create the interface:

public interface OnNewFragmentPressed {

void onNewFragmentPressed();

}

Then create a listener method of that interface.

public static class NewFragment extends Fragment {

    private OnNewFragmentPressed mListener;

Then in the onAttach() method of your Fragment, use the passed in Activity to make sure it implements the interface, and set the listener to that.

@Override

    public void onAttach(Activity activity) {

        super.onAttach(activity);

        try {

           mListener = (OnNewFragmentPressed) activity;

        } catch(ClassCastException e) {

           throw new ClassCastException(activity.toString() + ” didn’t implement OnNewFragmentPressed”);

        }

   }        

Now you can call methods of that interface, thereby interacting with your parent Activity.

  1. droiddevelop posted this
Blog comments powered by Disqus