Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Fragment.getArguments() return passed arguments after configuration changes?

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 ?

like image 566
Leonid Semyonov Avatar asked Apr 01 '14 11:04

Leonid Semyonov


1 Answers

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 :)

like image 118
Dawid Avatar answered Sep 21 '22 11:09

Dawid