Inter-Fragment communications – interfaces revisited

The main point of Fragments is that they have flexibility and reusability. If you remember, back in Chapter 12, Having a Dialogue with the User, when we were passing Note to and from a Fragment dialog, we added a method to Fragment and then called this method from the instance of the dialog in order to pass in the correct note that is to be shown. And when we added a new note in a dialog, we used getActivity() to get a reference to MainActivity in order to return the new note to be added to ArrayList of notes. Here is the code as a reminder:

// Get a reference to MainActivity MainActivity callingActivity = (MainActivity) getActivity(); // Pass newNote back to MainActivity callingActivity.createNewNote(newNote); ...

Get Android Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.