Best way to pass objects to fragments as parameter

1.5k Views Asked by At

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?

5

There are 5 best solutions below

3
Gennadii Saprykin On BEST ANSWER

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.

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 a Fragment.

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 than Parcelable because there is no serialization at all.

Other ways would be to write objects to database, pass their ids to a Fragment and 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 to String.

2
Marcos Vasconcelos On

You can do the Android way: Parcelable.

You can serialize then.

You can do the poor way : static

You can do the retained way: Create a Fragment with setRetainInstance(true) and save your objects references.

9
The Original Android On

I understand you don't want to use parcelable / serializable objects to a Bundle. I also agree with you since I got lazy, and my phone app is getting complicated.

Here's what you can do, and it works reliably.

  • Make a public method in your Fragment class, sample below.
  • Have the Activity, preferably no other place, call that public method. Remember Activity is always present, Fragments and Adapters may not due to its lifecycle.
  • The timing of the call is crucial if you're not using Bundles. I have used it without any problems.
  • The advantage of this technique is that it is fast, especially compared to Bundles. Many developers do not consider this however.
  • Note: If you are using simple fundamental Java types, do use Bundles! As suggested by Google.

Sample code:

public class MyFragment extends Fragment {
...
   public void setList(final ArrayList<String> arrayList) {
   ...
   }

In the Activity:

MyFragment fragment1 = MyFragment.newInstance(<parameters>);
fragment1.setList( arrayList );
0
N.T. On

Do you need to change the properties once they have been set on the fragment? If not, you can use setArguments(Bundle). If it is a fairly light object you can even skip implementing Parcelable and just set each property individually. The advantage is that the arguments are preserved upon orientation change. The disadvantage is that you need to call this method before attaching your fragment, hence it is not very useful once the fragment is in use.

2
Tomislav Emilov On

It's way too late for my answer, but if someone else is wondering. The recommended way is to use Parcelable or Serializable, but you can always do something like this:

public class ObjectManager {

    private static final String TAG = "ObjectManager";

    private static ObjectManager instance;

    private Object currentObject;

    public static ObjectManager getInstance() {
        if (instance == null)
            instance = new ObjectManager();
        return instance;
    }

    public Object getCurrentObject() {
        return currentObject;
    }

    public void setCurrentObject(Object object) {
        this.currentObject = object;
    }
}

And then use it: where you needed as long as your app is running

//Use on the object you would like to save
ObjectManager.getInstance().setCurrentObject(object);

//Get the instance from pretty much everywhere 
Object = ObjectManager.getInstance().getCurrentObject();

You can use it always, but it will be most likely to be useful, if you pass objects bigger than the Bundle max size.