Android Light/Dark Navigation Bar Color

I’ve pieced together the following to switch the color when dark mode (battery saver) is turned on or off. My questions are:

  • Do I need to wrap this in an if statement to avoid errors on older devices (my minSdkVersion is 22)?
  • How can I run updateDarkMode on start?
  • How can I run updateDarkMode on resume?

Here’s what I have so far. This is a bit out of my comfort zone.

import android.content.res.Configuration;
import android.view.Window;
import com.getcapacitor.BridgeActivity;
import android.os.Bundle;
public class MainActivity extends BridgeActivity {
  public void onConfigurationChanged(Configuration configuration) {
  private void updateDarkMode(Configuration configuration) {
    Window window = getWindow();
    String light = "#ffffff";
    String dark = "#222222";
    int currentNightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
    switch (currentNightMode) {
      case Configuration.UI_MODE_NIGHT_NO -> {
      case Configuration.UI_MODE_NIGHT_YES -> {