Skip to content Skip to sidebar Skip to footer

Passing Complex Data Directly Between Fragments?What's Wrong With The Code?

i want to pass data between 2 fragments.the data i am passing is an object of a class SongDetails.Here is the Code for the fragment which is passing the data ArrayList

Solution 1:

You can't pass data directly between 2 fragments. Instead you have to pass data from your fragment 1 to your activity through an interface that you must have previously created.

Then, inside the implemented method in your activity, you should retrieve the object reference to fragment 2 and call a public method that you created and do the job.

There is a good tutorial in Android official documentation:

Android Fragments


Solution 2:

To path any data from anywhere to anywhere else in your app use EventBus library. You can also do it by implementing observer design pattern but using library is far more easier since it also handles thread switching for you (just in case you need it) here is the link https://github.com/greenrobot/EventBus


Post a Comment for "Passing Complex Data Directly Between Fragments?What's Wrong With The Code?"