对不起我的英语不好
在通过\进行配置更改期间,我是否应该保存和恢复参数(由返回getArguments()
)?outState
savedInstanceState
或者getArguments()
即使在配置更改后也总是返回传递的参数?
对不起我的英语不好
在通过\进行配置更改期间,我是否应该保存和恢复参数(由返回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];
}
};
}
它被执行。如评论中所述,您可以阅读此内容:)。我还检查了代码,是的,它已保存。
在 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 会在配置更改后保留传递的参数。
您是否要更改此配置。在清单文件中,您可以进行此条目以避免更改