その画面の向きが変更された後、フラグメントにデータを保存して復元したいと考えています。
そのために、「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」変数が 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;
}
私は成功せずに多くのことを試みます。誰か私に何かアドバイスはありますか?前もって感謝します。