gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:41 25 4
gpt4 key购买 nike

对不起我的英语

配置更改期间,我是否应该保存恢复参数(由getArguments()返回)outState\savedInstanceState 吗?

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

最佳答案

简答

我只能回答


完整答案

您不必保存您的参数,它会自动完成。

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

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

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

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

关于android - 配置更改后 Fragment.getArguments() 是否返回传递的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22784904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com