I need to pass objects to my fragments in order to initialize them.
Currently I am doing this with ((MyActivity)getActivity()).getX(). (direct access to the activity)
However, I would like to pass the required objects as parameter.
I definitely do not want to add parcelable objects to the bundle, since they require an excessive amount of useless boilerplate code. My goal is to reduce complexity, not increasing it.
And I do not want to add serializable objects to the bundle, since they are slow and cause an unnecessary overhead.
What is the best way to pass objects to fragments? Any ideas to solve the problem in a more convenient way?
You write this code in your model classes which is separated from your activities and fragments. There is no complexity in implementing
Parcelable. And it is a common way to pass objects to aFragment.Any other solutions? Well, you still can do this
((MyActivity)getActivity()).getX()as long as your fragment is attached to your activity. In this case it is even faster thanParcelablebecause there is no serialization at all.Other ways would be to write objects to database, pass their ids to a
Fragmentand then use a query to retrieve objects.You can also use
SharedPreferences, but that's rarely used. For this you will need to convert your object toString.