0

我正在制作一个健身应用程序,允许用户制作健身活动列表。进行新活动时的选项之一是使用 Google 地图选择位置。这将打开一个新片段,并导致当前片段上的所有值被重置。为了防止这种情况发生,我保存了使用 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());
}

然后,当用户从 Google 地图返回时,我在 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"));
}

但是,这似乎不起作用。有谁知道为什么会发生这种情况?

4

2 回答 2

0

super.onSaveInstanceState(outState);在方法结束时调用

于 2016-11-10T19:05:00.873 回答
0

我通过将选定的值传递给 Google Map 片段,然后将这些值传递回原始片段来解决此问题:

// 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"));

我知道这不是最优雅的解决方案,但它确实有效。

于 2016-11-10T19:31:33.597 回答