samsap
samsap

Reputation: 60

Best practice to send data from activity to fragments

I wanted to know what are ways to send data from activity to fragment and what will be the best way to send.

I am aware of the fact that we can send data via bundle and setting data in the constructor. If so what are efficient ways to do so?

I felt bundle is an efficient way because you can send multiple data but while on rotation when your fragment is recreated then you can always get data by getArguments() while when you send data via the constructor of fragment it will not initialized again. Please tell me if my understanding is right?

Upvotes: 0

Views: 47

Answers (1)

PANKAJ KUMAR MAKWANA
PANKAJ KUMAR MAKWANA

Reputation: 86

Viewmodel comes to rescue in these situation. Viewmodel's one of the purpose is to survive configuration changes and in activity you can set your data into viewmodel and can access it in the fragment. Refer this https://developer.android.com/topic/libraries/architecture/viewmodel#sharing . Hope this helps.

Upvotes: 3

Related Questions