Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bc885205 authored by Justin Klaassen's avatar Justin Klaassen
Browse files

Prevent multiple unregisterReceiver calls in BaseActivity

Bug: 22104920
Change-Id: I8bfb0297c7b6f0559c9e732b192fa935f019d6f6
parent ee4ec4b5
Loading
Loading
Loading
Loading
+18 −13
Original line number Diff line number Diff line
@@ -44,12 +44,7 @@ public class BaseActivity extends AppCompatActivity {
    /**
     * {@link BroadcastReceiver} to update the background color whenever the system time changes.
     */
    private final BroadcastReceiver mOnTimeChangedReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
        }
    };
    private BroadcastReceiver mOnTimeChangedReceiver;

    /**
     * {@link ColorDrawable} used to draw the window's background.
@@ -70,12 +65,19 @@ public class BaseActivity extends AppCompatActivity {
    protected void onResume() {
        super.onResume();

        // Update the current background color periodically.
        // Register mOnTimeChangedReceiver to update current background color periodically.
        if (mOnTimeChangedReceiver == null) {
            final IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_TIME_TICK);
            filter.addAction(Intent.ACTION_TIME_CHANGED);
            filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        registerReceiver(mOnTimeChangedReceiver, filter);
            registerReceiver(mOnTimeChangedReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
                }
            }, filter);
        }

        // Ensure the background color is up-to-date.
        setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
@@ -86,7 +88,10 @@ public class BaseActivity extends AppCompatActivity {
        super.onPause();

        // Stop updating the background color when not active.
        if (mOnTimeChangedReceiver != null) {
            unregisterReceiver(mOnTimeChangedReceiver);
            mOnTimeChangedReceiver = null;
        }
    }

    @Override