belle tian
belle tian

Reputation: 897

can we call twice setArgument() on the Fragment?

I hold two Fragment instance in the activity ,add first fragment to activity , then replace second fragment to activity with setArgument() and addBackStack(), then press back button. now we return the first fragment , then we replace first to the second fragment which activity has hold once again , as the same with setArgument(), and it throws out a Exception ---- Fragment already active . what's wrong with this process?

Upvotes: 0

Views: 326

Answers (2)

Febi M Felix
Febi M Felix

Reputation: 2849

As per setArguments() source documentation, arguments supplied will be retained across fragment destroy and creation. So use getArguments() and then put bundle values to change the fields.

Upvotes: 1

Glenn
Glenn

Reputation: 12819

You can call it more than once or twice IF a Fragment is not attached to any Activity.

The code below is copied from Fragment.java

/**
 * Supply the construction arguments for this fragment.  This can only
 * be called before the fragment has been attached to its activity; that
 * is, you should call it immediately after constructing the fragment.  The
 * arguments supplied here will be retained across fragment destroy and
 * creation.
 */
 public void setArguments(Bundle args) {
        if (mIndex >= 0) {
            throw new IllegalStateException("Fragment already active");
        }
        mArguments = args;
 }

You can call the method as long as you want IF not attached to the activity

Upvotes: 0

Related Questions