Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Tom Finet
Tom Finet
7,027 Points

How to communicate between fragment and dialogue fragment effectively?

Hi guys,

I just wanted to know if there is a widely accepted way of communicating between fragments and dialogue fragments. In my app I have a sign up fragment which calls a date picker dialogue fragment on the press of a button.

In order to communicate between the two fragments I have created an interface in the date picker dialogue fragment, which is implemented in the sign up fragment and basically saves the inputed date using google datastore.

A problem with having the interface and listener in the dialogue fragment is that as soon as the fragment is recreated (rotation, etc.), it will lose that listener and an npe error will occur when you try and call it.

So how do I communicate between fragments and dialogue fragments efficiently so that no errors occur?