我正在制作一个健身应用程序,允许用户制作健身活动列表。进行新活动时的选项之一是使用 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"));
}
但是,这似乎不起作用。有谁知道为什么会发生这种情况?