Reputation: 5914
Can someone tell me what is the right way to call a function in one fragment from another fragment? I passed the object of one fragment to another fragemnt as serializable. but I dont think thats the right way.
I saw this question but the accepet answer just says activities should mediate all communications between fragments. can someone pls tell me what is the code to access an other fragment in the same activity by using activity as mediator?
Upvotes: 1
Views: 768
Reputation: 945
Please try reading the Guide to using fragments.
If you want Fragment_A to comunicate with Fragment_B, you should define an interface inside Fragment_A (which the parent activity has to implement) to send data from Fragment_A to parent activity and from the parent activity send that data to Fragment_B.
I hope I helped.
Upvotes: 1
Reputation: 2023
Create getter/setter methods for that shared Data in activity, In one fragment
((Appropriate Cast)getActivity).setSharedData(type Data) // saves data to be shared
In other fragment
((Appropriate Cast )getActivity).getSharedData() // returns shared data
Upvotes: 0