Fragment.getArguments()在配置更改后是否返回传递的参数?

Leo*_*nov 10 android android-configchanges android-fragments

对不起我的英语不好

我应该通过\来保存恢复配置更改getArguments()期间的参数(返回)吗?outStatesavedInstanceState

或者getArguments()总是在配置更改后返回传递的参数?

Daw*_*ozd 9

简答

我只能回答.


完整答案

您不必保存您的参数,它将自动完成.

这就是为什么:(也许我错了,但源代码说这个:)

在android支持库v4中

Android的SDK /演员/安卓/支持/ V4/src目录/ JAVA /安卓/支持/ V4 /应用/ Fragment.java

你有方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

在哪里mArgument:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

并在

/**
* 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];
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

它被执行了.在评论中你可以读到这是使用:).我也检查了代码,是的,它已保存.

在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;
}
Run Code Online (Sandbox Code Playgroud)

此外,我已经在Android上测试了几次,它仍然看起来它正在工作:)


lob*_*zik 5

是的,Android 会在配置更改后保留传递的参数。


小智 0

您是否要更改此配置。在清单文件中,您可以输入此条目以避免更改