10

对不起我的英语不好

在通过\进行配置更改期间,我是否应该保存恢复参数(由返回getArguments())?outStatesavedInstanceState

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

4

4 回答 4

10

简答

我只能回答


完整答案

你不必保存你的论点,它会自动完成。

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

在 android 支持库 v4

android-sdk/extras/android/support/v4/src/java/android/support/v4/app/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);
}

在哪里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;
}

而在

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

它被执行。如评论中所述,您可以阅读此内容:)。我还检查了代码,是的,它已保存。

在 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;
}

此外,我在 android 上对其进行了几次测试,它看起来仍然可以正常工作:)

于 2015-03-13T13:22:21.980 回答
5

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

于 2014-10-23T13:09:30.357 回答
3

似乎确实如此。在这里检查

它说;

提供此片段的构造参数。这只能在片段附加到其活动之前调用;也就是说,您应该在构建片段后立即调用它。此处提供的参数将在片段销毁和创建过程中保留。

于 2016-10-11T12:35:00.017 回答
0

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

于 2014-04-01T12:10:38.860 回答