11

Sorry for my English

Should I save and restore arguments (returned by getArguments()) during configuration changes via outState \ savedInstanceState ?

Or getArguments() always returns passed arguments even after configuration changes ?

Leonid Semyonov
  • 473
  • 2
  • 6
  • 18

4 Answers4

10

Short Answer

I can only answer No.


Full answer

You don't have to save your arguments it will be done automagicly.

This is why: (maybe im wrong but source code says this:)

In android support library v4

android-sdk/extras/android/support/v4/src/java/android/support/v4/app/Fragment.java

You have method:

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(mClassName);
    dest.writeInt(mIndex);
    dest.writeInt(mFromLayout ? 1 : 0);
    dest.writeInt(mFragmentId);
    dest.writeInt(mContainerId);
    dest.writeString(mTag);
    dest.writeInt(mRetainInstance ? 1 : 0);
    dest.writeInt(mDetached ? 1 : 0);
    dest.writeBundle(mArguments);//<---------------------------- Look here
    dest.writeBundle(mSavedFragmentState);
}

Where mArgument is:

/**
 * 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;
}

And in

/**
* State information that has been retrieved from a fragment instance
* through {@link FragmentManager#saveFragmentInstanceState(Fragment)
* FragmentManager.saveFragmentInstanceState}.
*/
public static class SavedState implements Parcelable {
    final Bundle mState;

    SavedState(Bundle state) {
        mState = state;
    }

    SavedState(Parcel in, ClassLoader loader) {
        mState = in.readBundle();
        if (loader != null && mState != null) {
            mState.setClassLoader(loader);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeBundle(mState);
    }

    public static final Parcelable.Creator<SavedState> CREATOR
            = new Parcelable.Creator<SavedState>() {
        public SavedState createFromParcel(Parcel in) {
            return new SavedState(in, null);
        }

        public SavedState[] newArray(int size) {
            return new SavedState[size];
        }
    };
}

it is executed. As in comment you can read this is used :). Also i checked the code and yes it is saved.

In FragmentManager.java

@Override
public Fragment.SavedState saveFragmentInstanceState(Fragment fragment) {
    if (fragment.mIndex < 0) {
        throwException( new IllegalStateException("Fragment " + fragment
                + " is not currently in the FragmentManager"));
    }
    if (fragment.mState > Fragment.INITIALIZING) {
        Bundle result = saveFragmentBasicState(fragment);
        return result != null ? new Fragment.SavedState(result) : null;
    }
    return null;
}

Also i have tested it few times on android and it still looks that it is working :)

Community
  • 1
  • 1
Gelldur
  • 11,187
  • 7
  • 57
  • 68
  • Is this documented anywhere? While I doubt they'd change this (it'd probably break tons of apps), it'd be good to have a guarantee about it. – Michael Marvick Nov 13 '15 at 18:23
  • From zgulser response: ["will be retained across fragment destroy and creation"](https://developer.android.com/reference/android/support/v4/app/Fragment#setarguments) – Gelldur Nov 30 '18 at 09:17
5

Yes, Android persists passed arguments after configuration changes.

lobzik
  • 10,974
  • 1
  • 27
  • 32
3

It seems it does. Check here

It says;

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.

stdout
  • 2,471
  • 2
  • 31
  • 40
0

do you want to make this configuration change or not. in manifest file you can make this entry to avoid changes

gandhi
  • 122
  • 6
  • You misunderstood me. I want to know whether or not `getArguments()` always returns passed arguments even after configuration changes. – Leonid Semyonov Apr 01 '14 at 12:22
  • If you have set `setRetainInstance(true);` then you will be okay. Otherwise yes, you need to store and retrieve the way you have suggested. – JVillella Oct 23 '14 at 02:11
  • Data that you send as argument into setArguments is persistent. So getArguments() will return it after fragment is recreated or configuration changes. Note: getArguments() will work correctly without "setRetainInstance" – Oleksandr B Aug 31 '16 at 17:48