null pointer
null pointer

Reputation: 5914

what is the rigth way to call a function in one fragment from another fragemt?

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

Answers (2)

Chris Margonis
Chris Margonis

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

baboo
baboo

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

Related Questions