[英]Can not display data in fragment even if savedInstanceState is not null
I would like to save and restore data in an fragment after that screen orentation has changed. 在屏幕方向更改后,我想在片段中保存和恢复数据。
For that, I use "onSaveInstanceState" method to save my data. 为此,我使用“ onSaveInstanceState”方法保存数据。
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("date", dateTextView.getText().toString());
outState.putString("time", timeTextView.getText().toString());
outState.putString("hour", hour);
outState.putString("minute", minute);
outState.putString("topicGroup", currentTopicGroup);
outState.putString("topic", currentTopicTitle);
outState.putString("level",currentLevelName);
outState.putDouble("totalPrice", totalPrice);
outState.putString("activityTitle", getActivity().getTitle().toString());
super.onSaveInstanceState(outState);
}
But when I try to restore/display my data with the "onCreateView" method like below, nothing happens even if the "savedInstanceState" variable is not null (my logs display the right value). 但是,当我尝试使用如下所示的“ onCreateView”方法还原/显示我的数据时,即使“ savedInstanceState”变量不为null(我的日志显示正确的值),也没有任何反应。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_create_new_lesson, container, false);
timeTextView = (TextView) view.findViewById(R.id.time_picker_text_view);
dateTextView = (TextView) view.findViewById(R.id.date_picker_text_view);
hourSpinner = (Spinner) view.findViewById(R.id.hour_spinner);
minuteSpinner = (Spinner) view.findViewById(R.id.minut_spinner);
topicGroupSpinner = (Spinner) view.findViewById(R.id.topic_group_spinner);
topicSpinner = (Spinner) view.findViewById(R.id.topic_spinner);
levelSpinner = (Spinner) view.findViewById(R.id.level_spinner);
totalPriceTextView = (TextView) view.findViewById(R.id.total_price_text_view);
datePickerButton = (Button) view.findViewById(R.id.date_picker_button);
timePickerButton = (Button) view.findViewById(R.id.time_picker_button);
createNewLessonButton = (Button) view.findViewById(R.id.create_new_lesson_button);
if (savedInstanceState != null) {
String time = savedInstanceState.getString("time");
Log.i("TIME", time);
getActivity().setTitle(time);
dateTextView.setText(savedInstanceState.getString("date"));
timeTextView.setText(savedInstanceState.getString("time"));
int hourPosition = getIndexByString(hourSpinner, savedInstanceState.getString("hour"));
int minutePosition = getIndexByString(minuteSpinner, savedInstanceState.getString("minute"));
int topicGroupPosition = getIndexByString(topicGroupSpinner, savedInstanceState.getString("topicGroup"));
hourSpinner.setSelection(hourPosition);
minuteSpinner.setSelection(minutePosition);
topicGroupSpinner.setSelection(topicGroupPosition);
}
return view;
}
I try many things without success. 我尝试许多事情都没有成功。 Would anyone have any advice to give me ? 有人可以给我什么建议吗? Thanks in advance. 提前致谢。
The problem is in the place where you are trying to restore the state of your views. 问题出在您试图还原视图状态的地方。 Take a look: 看一看:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
if (savedInstanceState != null) {
//Restore the fragment's state here
}
}
...
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Save the fragment's state here
}
The topic has already been commented on StackOverflow in depth. 该主题已经在StackOverflow上进行了深入评论。 Just take a look and you will find a lot about the savedState and restoring it. 看看,您会发现很多关于saveState并对其进行恢复的信息。 Fragment's SavedState and Restoring It 片段的SavedState并还原
The problem is every time a new Fragment
instance with empty bundle data is created and added. 问题是每次创建并添加带有空捆绑数据的新Fragment
实例时。 While adding a new Fragment
in the activity do as below 在活动中添加新Fragment
时,请执行以下操作
In onCreate()
of activity or wherever you add the fragment do as below 在活动的onCreate()
或添加片段的任何地方,请执行以下操作
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.frag_container, new Frag1()).commit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.