8.3.2. Communicating Data Between a Fragment and a Host Activity
To communicate data between Fragment
s and a host Activity
or the Activity
’s other Fragment
s, it’s considered best practice to do so through the host Activity
—this makes the Fragment
s more reusable, because they do not refer to one another directly. Typically, each Fragment
defines an interface of callback methods that are implemented in the host Activity
. We’ll use this technique to enable this app’s MainActivity
to be notified when the user selects a contact to display, touches an action bar item (, or ), or finishes editing an existing contact or adding a new one.
Get Android™ How to Program, Second Edition 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.