0

I am making a fitness app that allows users to make lists of fitness events. One of the options when making a new event is to select the location using a Google Map. This opens a new fragment, and causes all values on the current fragment to be reset. To prevent this from happening, I save any values that have been set by using onSaveInstanceState():

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mEventStr != null)
        outState.putString("event", mEventStr);
    if (mSelectStartDateTxtVw != null)
        outState.putString("startDate", mSelectStartDateTxtVw.getText().toString());
    if (mSelectStartTimeTxtVw != null)
        outState.putString("startTime", mSelectStartTimeTxtVw.getText().toString());
    if (mSelectEndDateTxtVw != null)
        outState.putString("endDate", mSelectEndDateTxtVw.getText().toString());
    if (mSelectEndTimeTxtVw != null)
        outState.putString("endTime", mSelectEndTimeTxtVw.getText().toString());
    if (mEventType != null)
        outState.putString("eventType", mEventType);
    if (mEventDescriptionEdTxt != null)
        outState.putString("eventDescription", mEventDescriptionEdTxt.getText().toString());
}

Then, when the user has returned from the Google Map, I restore these values in onCreateView():

if (savedInstanceState != null) {
    mEventStr = savedInstanceState.getString("event");
    mSelectStartDateTxtVw.setText(savedInstanceState.getString("startDate"));
    mSelectStartTimeTxtVw.setText(savedInstanceState.getString("startTime"));
    mSelectEndDateTxtVw.setText(savedInstanceState.getString("endDate"));
    mSelectEndTimeTxtVw.setText(savedInstanceState.getString("endTime"));
    mEventType = savedInstanceState.getString("eventType");
    mEventDescriptionEdTxt.setText(savedInstanceState.getString("eventDescription"));
}

This, however, does not seem to work. Does anyone have an idea of why this may be happening?

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
jtmaher2
  • 482
  • 5
  • 19
  • Possible duplicate of [Once for all, how to correctly save instance state of Fragments in back stack?](http://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack) – David Medenjak Nov 10 '16 at 18:33
  • "This opens a new fragment" -- please explain **precisely** what you are doing to "open" a fragment. "and causes all values on the current fragment to be reset" -- please explain **precisely** what you are doing that is causing "all values" (fields?) on whatever "the current fragment" is "to be reset". The saved instance state `Bundle` is for configuration changes and related scenarios. – CommonsWare Nov 10 '16 at 18:48
  • If you replace a fragment with another fragment, the saved state will be cleared if you don't put it on the backstack.... ..... – EpicPandaForce Nov 10 '16 at 20:02

2 Answers2

0

Call the super.onSaveInstanceState(outState); at the end of the method

pry1
  • 81
  • 3
0

I fixed this problem by passing the selected values to the Google Map fragment, and then passing these values back to the original fragment:

// CreateEventFragment.java
Bundle bundle = new Bundle();
if (mEventStr != null)
    outState.putString("event", mEventStr);
if (mSelectStartDateTxtVw != null)
    outState.putString("startDate", mSelectStartDateTxtVw.getText().toString());
if (mSelectStartTimeTxtVw != null)
    outState.putString("startTime", mSelectStartTimeTxtVw.getText().toString());
if (mSelectEndDateTxtVw != null)
    outState.putString("endDate", mSelectEndDateTxtVw.getText().toString());
if (mSelectEndTimeTxtVw != null)
    outState.putString("endTime", mSelectEndTimeTxtVw.getText().toString());
if (mEventType != null)
    outState.putString("eventType", mEventType);
if (mEventDescriptionEdTxt != null)
    outState.putString("eventDescription", mEventDescriptionEdTxt.getText().toString());

// GoogleMapFragment.java
Bundle inBundle = getArguments();
Bundle outBundle = new Bundle();
outBundle.putString("event", inBundle.getString("event"));
outBundle.putString("startDate", inBundle.getString("startDate"));
outBundle.putString("startTime", inBundle.getString("startTime"));
outBundle.putString("endDate", inBundle.getString("endDate"));
outBundle.putString("endTime", inBundle.getString("endTime"));
outBundle.putString("eventType", inBundle.getString("eventType"));
outBundle.putString("eventDescription", inBundle.getString("eventDescription"));

I know this is not the most elegant solution, but it works.

jtmaher2
  • 482
  • 5
  • 19