I have two Fragments in my Activity: fragment A with button X and fragment B with button Y.
How can I change button X‘s background image when I click button B? Is it possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From the documentation,
That being said, what you want to do is create event callbacks to the activity. A good way to do that is to define a callback interface inside the fragment and require that the host activity implement it. When the activity receives a callback through the interface, it can share the information with other fragments in the layout as necessary. This is the recommended way to share events between two separate
Fragments–that is, sharing the event through the activity.Check out the link above… it provides a couple nice examples. If you are still having trouble, let me know and maybe I can be more explicit.
Edit #1:
Let’s say you click a button in fragment
Aand you want this to cause changes to a button in fragmentB. Here’s some sample code illustrating the concept:The callback interface:
The activity:
Fragment A:
Edit #2:
Now, you might be wondering, “Why would anyone ever go through all of this trouble? What’s the point of creating a separate activity callback method when you could just have fragment
Adirectly manipulate fragmentB?”The main reason you want to do this is to ensure that each fragment is designed as a modular and reusable activity component. This is especially important because a modular fragment allows you to change your fragment combinations for different screen sizes. When designing your application to support both tablets and handsets, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset, it might be necessary to separate fragments to provide a single-pane UI when more than one cannot fit within the same activity. Making use of activity callbacks ensures that you will easily be able to reuse your fragments in situations where fragment
Bis not visible on the screen. For example, if you are on a handheld device and there is not enough room to display fragmentB, then you can easily have your activity check to see if fragmentBis currently being shown on the screen.Sorry if this isn’t clear… I’m finding it difficult to describe :P. Working your way through this tutorial might help… Activity callbacks make your life especially easier as a developer when you are working with interactive multi-pane layouts.