Fix crash if timezone is changed and "Don't keep activities" mode is on

The issues is that TimeZoneData is being loaded with LoaderManager on
every fragment's onCreate(). At the same time this fragment has
onActivityResult() handler that accesses TimeZoneData and relies on the
fact that it has been already initialized prior this call. So when you
enable "Don't keep activities" mode, the TimeZoneSettings fragment [1]
that holds TimeZoneData is immediately destroyed after launching time zone
picker fragment [2] (which result [1] waits for), and when android calls
onActivityResult() for [1] (after time zone has been picked in [2]),
Android has to recreate [1] from scratch. So you get onCreate() called and
immidiately after that onActivityResult() called, and at this point we get
the crash, because the loader hasn't finished yet and onActivityResult()
tries to access uninitialized mTimeZoneData.

This CL fixes this crash issue by postponing
mTimeZoneData.lookupCountryTimeZones call until mTimeZoneData is loaded
by loader. It is done through saving data intent in onActivityResult()
and calling onZonePickerRequestResult() when time zone data is loaded.

Bug: 112351583
Test: m Settings droid successfully builds, manual testing with "Don't
keep activities" mode enabled

Change-Id: Id30d36dc17a0de96ba871f9d0f4a2686479d0eeb
This commit is contained in:
Nikita Iashchenko
2019-01-28 21:16:31 +00:00
parent 0b880088a4
commit 8db2d30fab

View File

@@ -69,6 +69,7 @@ public class TimeZoneSettings extends DashboardFragment {
private Locale mLocale;
private boolean mSelectByRegion;
private TimeZoneData mTimeZoneData;
private Intent mPendingZonePickerRequestResult;
private String mSelectedTimeZoneId;
private TimeZoneInfo.Formatter mTimeZoneInfoFormatter;
@@ -136,12 +137,10 @@ public class TimeZoneSettings extends DashboardFragment {
switch (requestCode) {
case REQUEST_CODE_REGION_PICKER:
case REQUEST_CODE_ZONE_PICKER: {
String regionId = data.getStringExtra(RegionSearchPicker.EXTRA_RESULT_REGION_ID);
String tzId = data.getStringExtra(RegionZonePicker.EXTRA_RESULT_TIME_ZONE_ID);
// Ignore the result if user didn't change the region or time zone.
if (!Objects.equals(regionId, use(RegionPreferenceController.class).getRegionId())
|| !Objects.equals(tzId, mSelectedTimeZoneId)) {
onRegionZoneChanged(regionId, tzId);
if (mTimeZoneData == null) {
mPendingZonePickerRequestResult = data;
} else {
onZonePickerRequestResult(mTimeZoneData, data);
}
break;
}
@@ -166,8 +165,11 @@ public class TimeZoneSettings extends DashboardFragment {
mTimeZoneData = timeZoneData;
setupForCurrentTimeZone();
getActivity().invalidateOptionsMenu();
if (mPendingZonePickerRequestResult != null) {
onZonePickerRequestResult(timeZoneData, mPendingZonePickerRequestResult);
mPendingZonePickerRequestResult = null;
}
}
}
private void startRegionPicker() {
@@ -226,9 +228,17 @@ public class TimeZoneSettings extends DashboardFragment {
updatePreferenceStates();
}
private void onRegionZoneChanged(String regionId, String tzId) {
private void onZonePickerRequestResult(TimeZoneData timeZoneData, Intent data) {
String regionId = data.getStringExtra(RegionSearchPicker.EXTRA_RESULT_REGION_ID);
String tzId = data.getStringExtra(RegionZonePicker.EXTRA_RESULT_TIME_ZONE_ID);
// Ignore the result if user didn't change the region or time zone.
if (Objects.equals(regionId, use(RegionPreferenceController.class).getRegionId())
&& Objects.equals(tzId, mSelectedTimeZoneId)) {
return;
}
FilteredCountryTimeZones countryTimeZones =
mTimeZoneData.lookupCountryTimeZones(regionId);
timeZoneData.lookupCountryTimeZones(regionId);
if (countryTimeZones == null || !countryTimeZones.getTimeZoneIds().contains(tzId)) {
Log.e(TAG, "Unknown time zone id is selected: " + tzId);
return;