Setting result earlier than onPause

Setting the result in onPause is too late for delivering it to the
requesting activity. Changing to setting the result any time the
uncommitted state is written. The state will still be committed in
onPause only.

Test: Manually with a test app using startActivityForResult.

Bug: 188477366
Change-Id: I676c99e7d2a64b644cef60ecd14728d4d49180c3
This commit is contained in:
Suprabh Shukla
2021-05-18 22:08:01 -07:00
parent 39e7cb2721
commit 63fd87a13a

View File

@@ -81,6 +81,9 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
if (savedInstanceState != null) { if (savedInstanceState != null) {
mUncommittedState = (Boolean) savedInstanceState.get(UNCOMMITTED_STATE_KEY); mUncommittedState = (Boolean) savedInstanceState.get(UNCOMMITTED_STATE_KEY);
if (mUncommittedState != null && isAppSpecific()) {
setResult(mUncommittedState ? RESULT_OK : RESULT_CANCELED);
}
} }
addPreferencesFromResource(R.xml.alarms_and_reminders); addPreferencesFromResource(R.xml.alarms_and_reminders);
mSwitchPref = findPreference(KEY_SWITCH); mSwitchPref = findPreference(KEY_SWITCH);
@@ -97,9 +100,11 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
final boolean checked = (Boolean) newValue;
if (preference == mSwitchPref) { if (preference == mSwitchPref) {
mUncommittedState = checked; mUncommittedState = (Boolean) newValue;
if (isAppSpecific()) {
setResult(mUncommittedState ? RESULT_OK : RESULT_CANCELED);
}
refreshUi(); refreshUi();
return true; return true;
} }
@@ -125,6 +130,11 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
newState ? 1 : 0); newState ? 1 : 0);
} }
private boolean isAppSpecific() {
return Settings.AlarmsAndRemindersAppActivity.class.getName().equals(
getIntent().getComponent().getClassName());
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
@@ -133,10 +143,6 @@ public class AlarmsAndRemindersDetails extends AppInfoWithHeader
} }
if (mPermissionState != null && mUncommittedState != null if (mPermissionState != null && mUncommittedState != null
&& mUncommittedState != mPermissionState.isAllowed()) { && mUncommittedState != mPermissionState.isAllowed()) {
if (Settings.AlarmsAndRemindersAppActivity.class.getName().equals(
getIntent().getComponent().getClassName())) {
setResult(mUncommittedState ? RESULT_OK : RESULT_CANCELED);
}
setCanScheduleAlarms(mUncommittedState); setCanScheduleAlarms(mUncommittedState);
logPermissionChange(mUncommittedState, mPackageName); logPermissionChange(mUncommittedState, mPackageName);
mUncommittedState = null; mUncommittedState = null;