user3697901
user3697901

Reputation: 21

Is the bundle in fragment getArgs() the same object that is passed through setArgs()?

If I set arguments of a fragment myfragment to a Bundle mybundle, am I guaranteed that if I change the contents of mybundle later down the road, myfragment's call to getArguments() will be consistent with the contents of mybundle?

i.e.

mybundle.putString("background", "red");
myfragment.setArguments(mybundle);

... later ...

mybundle.putString("background", "orange");
myfragment.createLayoutFromBundle(myfragment.getArguments());

Upvotes: 0

Views: 173

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007296

The arguments Bundle is part of the fragment's saved instance state. If the fragment is destroyed and re-created, your newly-created fragment will have an arguments Bundle with the same contents as did the original Bundle. However, the Bundle object may be different.

This will most easily seen when:

  • the user is in your fragment, with the Bundle
  • the user presses HOME
  • your process is terminated (you can test this via DDMS)
  • the user returns to your fragment via the recent-tasks list

You will have the same data in the Bundle as before, but the Bundle object will be newly-created.

Upvotes: 0

matiash
matiash

Reputation: 55360

Yes. Check the source code for the Fragment class. The bundle is not copied or anything, just returned as-is.

    public void setArguments(Bundle args) {
        if (mIndex >= 0) {
            throw new IllegalStateException("Fragment already active");
        }
        mArguments = args;
    }

/**
* Return the arguments supplied when the fragment was instantiated,
* if any.
*/
    final public Bundle getArguments() {
        return mArguments;
    }

Upvotes: 1

Related Questions