title: Infinite loop on settheme()
body: <p>I am trying to change the app theme from the fragment using <code>setTheme()</code> method of an activity. But somehow its looping forever while trying to change. Here is my <code>setTheme()</code> method calls.</p>
  <pre><code>//XYZ Fragment class
  
  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  
      if (mThemeName.equalsIgnoreCase(&quot;Mode 1&quot;)) {
          AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
          getActivity().setTheme(R.style.Theme_One);
      } else  {
          getActivity().setTheme(R.style.Theme_Two);
      }
  
      super.onViewCreated(view, savedInstanceState);
  }     
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      //binding object of this fragment
      binding = FragmentXYZBinding.inflate(inflater);
  
      binding.switch.setOnCheckedChangeListener((buttonView, isChecked) -&gt; {
  
          if(isChecked){
              setTheme(&quot;Mode 2&quot;);
          }else {
              setTheme(&quot;Mode 1&quot;);
          }
      });
  
      return inflater.inflate(R.layout.fragment_setting, container, false);
  }
  
  public void setTheme(String name) {
  
      SharedPreferences preferences = getActivity().getSharedPreferences(&quot;app&quot;, Context.MODE_PRIVATE);
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString(&quot;ThemeName&quot;, name);
      editor.apply();
  
      // If i call this recreate() then it goes into infinite loop 
      // but without calling this I can't change theme on runtime
      //getActivity().recreate();
  }
  </code></pre>
  <p>Any idea about what could be wrong in here ?</p>