Shane Claycomb
Shane Claycomb

Reputation: 15

Kotlin sending data to fragments

I am making an app that pulls from a mySQL database. I have a main activity which holds a drawer with a bunch of fragments. If I wanted to make a query and save information in main activity in an array or something, would it be accessible in all of my fragments? I've had trouble trying to send data from fragments, and just read that fragments in the same activity all share the same data. Could someone confirm that if I made an array in mainActivity, that the array would be accessable and mutable by the fragments? (this is my first post, sorry if it's bad)

Upvotes: 0

Views: 140

Answers (1)

Androii
Androii

Reputation: 74

Why don't you move the array from main activity and create repo which will provide data on request for fragments and main activity. Change in orientation can cause the data in main activity to be wiped out if you are not handling the orientation. The best way I would recommend is to use view model for the main activity and use that for fragments. Share data between fragments

Hope it helped. Happy coding :)

Upvotes: 1

Related Questions