When a fragment starts up, the onAttach() method delivers the reference to the containing activity. If that activity has implemented the fragment's interface, you can save the activity reference. Then, at the appropriate moment, the fragment can send data back to the activity by calling the interface's methods.
- [Instructor] Once you've set up the architecture…for callback methods, it's easy to pass messages…from the fragment to the containing activity.…I'm starting in a project named "SendMessage",…which picks up where the last project left off.…In the detail fragment, I've defined an interface…that I named "FragmentListener"…that included a single abstract method…named "OnFragmentFinish".…And I implemented that interface in the main activity…and implemented the one method down here.…
And all the method is doing right now…is using logcat to trace the message that's received.…In the detail fragment, I'll add code to the done method…that's called when the user touches the button.…First, I'll make sure that the listener object…has been defined correctly.…It should be done,…because I got that reference in the OnAttach method up here.…And so, once again, I'll use an assertion error.…I'll do a little bit of conditional code,…and I'll say, "If mListener is null,…then I'll throw the error".…
If I get down to this part,…I can now send the message to the activity.…
Author
Released
9/1/2016- Configuring Android Studio
- Understanding fragments
- Creating a fragment class and layout
- Adding and removing fragments with Java
- Creating layouts for multiple screens
- Understanding arguments and callback methods
- Passing arguments to a fragment
- Choosing layout at runtime
- Displaying dialogs with fragments
- Using fragments for managing dialogs, shared preferences, and more
Skill Level Intermediate
Duration
Views
-
Introduction
-
Welcome1m 4s
-
What you should know1m 50s
-
Using the exercise files1m 39s
-
-
1. Getting Started
-
Understanding fragments3m 17s
-
2. Display Fragments in Activities
-
Display a fragment with XML3m 16s
-
Add a fragment with Java3m 50s
-
3. Manage Fragments at Runtime
-
Pass arguments to a fragment5m 18s
-
Choose layout at runtime3m 21s
-
-
4. Communication between Fragments and Activities
-
5. Display Dialogs with Fragments
-
Display a custom dialog5m 1s
-
Pass arguments to the dialog4m 16s
-
6. Other Uses of Fragments
-
Create a ViewPager adapter4m 53s
-
Conclusion
-
Next steps1m 11s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Send a message from a fragment