我正在制作一个健身应用程序,允许用户制作健身活动列表。制作新活动时的其中一个选项是使用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 Map返回时,我会在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"));
}
然而,这似乎不起作用。有没有人知道为什么会这样?
答案 0 :(得分:0)
在方法结束时调用import MapKit
let kEarthRadius = 6378137.0
// CLLocationCoordinate2D uses degrees but we need radians
func radians(degrees: Double) -> Double {
return degrees * M_PI / 180;
}
func regionArea(locations: [CLLocationCoordinate2D]) -> Double {
guard locations.count > 2 else { return 0 }
var area = 0.0
for i in 0..<locations.count {
let p1 = locations[i > 0 ? i - 1 : locations.count - 1]
let p2 = locations[i]
area += radians(degrees: p2.longitude - p1.longitude) * (2 + sin(radians(degrees: p1.latitude)) + sin(radians(degrees: p2.latitude)) )
}
area = -(area * kEarthRadius * kEarthRadius / 2);
return max(area, -area) // In order not to worry about is polygon clockwise or counterclockwise defined.
}
答案 1 :(得分: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"));
我知道这不是最优雅的解决方案,但它确实有效。