0

在屏幕方向更改后,我想在片段中保存和恢复数据。

为此,我使用“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;
}

我尝试了很多事情都没有成功。有人可以给我任何建议吗?提前致谢。

4

2 回答 2

0

问题是每次Fragment创建和添加具有空包数据的新实例时。在活动中添加新内容Fragment时,请执行以下操作

onCreate()活动中或添加片段的任何地方,如下所示

if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction().add(R.id.frag_container, new Frag1()).commit();
}
于 2017-02-14T15:20:38.213 回答
0

问题出在您试图恢复视图状态的地方。看一看:

@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
}

该主题已经在 StackOverflow 上进行了深入评论。只要看一下,您就会发现很多关于已保存状态和恢复它的信息。Fragment 的 SavedState 和恢复它

于 2017-02-14T15:17:41.583 回答