Move receiver registration to onPause/onResume for MobileNetworkSettings.
Moving registration and unregistration of receivers on the
MobileNetworkSettings page to the onPause/onResume methods.
Previously this was done in onCreate and onDestroy.
Also, adding null checks in the receivers to ensure that they won't try
to update the UI when the activity is null.
Test: Manual
Bug: 109899326
Change-Id: Ic5a23c33d8162152e99e033eab9046cd70051482
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 1f406ea..a5bee73 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -720,13 +720,6 @@
int max = mSubscriptionManager.getActiveSubscriptionInfoCountMax();
mActiveSubInfos = new ArrayList<SubscriptionInfo>(max);
- IntentFilter intentFilter = new IntentFilter(
- TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
- activity.registerReceiver(mPhoneChangeReceiver, intentFilter);
-
- activity.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false,
- mDpcEnforcedContentObserver);
-
Log.i(LOG_TAG, "onCreate:-");
}
@@ -753,6 +746,10 @@
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "onReceive:");
+ if (getActivity() == null || getContext() == null) {
+ // Received broadcast and activity is in the process of being torn down.
+ return;
+ }
// When the radio changes (ex: CDMA->GSM), refresh all options.
updateBody();
}
@@ -766,6 +763,10 @@
@Override
public void onChange(boolean selfChange) {
Log.i(LOG_TAG, "DPC enforced onChange:");
+ if (getActivity() == null || getContext() == null) {
+ // Received content change and activity is in the process of being torn down.
+ return;
+ }
updateBody();
}
}
@@ -774,11 +775,6 @@
public void onDestroy() {
unbindNetworkQueryService();
super.onDestroy();
- if (getActivity() != null) {
- getActivity().unregisterReceiver(mPhoneChangeReceiver);
- getActivity().getContentResolver().unregisterContentObserver(
- mDpcEnforcedContentObserver);
- }
}
@Override
@@ -815,6 +811,13 @@
mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+ final Context context = getActivity();
+ IntentFilter intentFilter = new IntentFilter(
+ TelephonyIntents.ACTION_RADIO_TECHNOLOGY_CHANGED);
+ context.registerReceiver(mPhoneChangeReceiver, intentFilter);
+ context.getContentResolver().registerContentObserver(ENFORCE_MANAGED_URI, false,
+ mDpcEnforcedContentObserver);
+
Log.i(LOG_TAG, "onResume:-");
}
@@ -1150,6 +1153,10 @@
mSubscriptionManager
.removeOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+
+ final Context context = getActivity();
+ context.unregisterReceiver(mPhoneChangeReceiver);
+ context.getContentResolver().unregisterContentObserver(mDpcEnforcedContentObserver);
if (DBG) log("onPause:-");
}