0

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.

@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).

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

Wivi0
  • 11
  • 4

2 Answers2

0

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. Just take a look and you will find a lot about the savedState and restoring it. Fragment's SavedState and Restoring It

Community
  • 1
  • 1
T.Dimitrov
  • 157
  • 2
  • 7
0

The problem is every time a new Fragment instance with empty bundle data is created and added. While adding a new Fragment in the activity do as below

In onCreate() of activity or wherever you add the fragment do as below

if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction().add(R.id.frag_container, new Frag1()).commit();
}
arjun
  • 3,514
  • 4
  • 27
  • 48