You can communicate from an activity to a fragment by adding public methods to the fragment class. Then at runtime, get a reference to the fragment by the ID of its placeholder View, or by a string-based tag that you identify when you instantiate the fragment. You can then call those public methods from the activity.
- [Instructor] Once a fragment has been displayed…in an activity, you can call that fragment object methods,…but first you need to get a reference to the fragment.…You can't create a reference and hold onto it…for the lifetime of the activity.…That's because of configuration changes,…such as changes in orientation.…Instead, each time you want to reference a fragment object,…you need to get a reference to it at that moment.…I'll demonstrate this in this project named Update Fragment.…In this project, I once again have a single fragment…and a method called Add Fragment One…that's replacing the fragment in the placeholder.…
I'll run the application and show its current behavior.…I'll click Fragment One,…and see the text from that fragment.…Now, when I click this second button,…now labeled Update Fragment,…instead of replacing the fragment with another fragment,…I simply want to update the existing fragment…by calling one of its methods.…First, I'll go to the fragment class itself…and I'll add a new public method.…
- Define Android market fragmentation.
- Determine how to set minimum and target platforms.
- Recognize how to implement alternative resource directories.
- Identify how to create bitmap resources.
- Demonstrate how to control a fragment using Java code.