在屏幕方向更改后,我想在片段中保存和恢复数据。
为此,我使用“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);
}
但是当我尝试使用下面的“onCreateView”方法恢复/显示我的数据时,即使“savedInstanceState”变量不为空(我的日志显示正确的值),也不会发生任何事情。
@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;
}
我尝试了很多事情都没有成功。有人可以给我任何建议吗?提前致谢。