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

时间:2014-04-01 11:45:24

标签: android android-fragments android-configchanges

抱歉我的英文

我应该配置更改期间通过getArguments() \ {保存还原参数(由outState返回) {1}}?

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

4 个答案:

答案 0 :(得分:8)

简答

我只能回答


完整答案

您不必保存您的论据,它将自动完成。

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

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

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上测试了几次,它仍然看起来它正在工作:)

答案 1 :(得分:4)

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

答案 2 :(得分:3)

似乎确实如此。查看here

它说;

  

提供此片段的构造参数。这只能是   在片段附加到其活动之前调用;那是,   你应该在构造片段后立即调用它。该   这里提供的参数将保留在片段destroy和   创建

答案 3 :(得分:0)

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