O'Reilly logo

Android Programming for Beginners by John Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required