title: Android: Setting locale programmatically in the new way alters context
body: <p>I know there is a lot of discussion about this topic, and I have done my homework and previously read many stackoverflow posts but none of them seems solve my problem.</p>
  
  <p>I have a setLocale function that launch on every activity onCreate (not sure if its the best way to do it but is the way I'm doing it right now) and setLocale creates and returns a new context with custom locale configuration applied and it works (when you call getString the text in the correct language is return), but the fact is that this new context (context = context.createConfigurationContext(config) in setLocale) is of type <strong>ContextImpl?</strong> and what I expect it to be is the Activity which called setLocale, because I then run methods that need an activity in context (a context of type activity) but as it's ContextImpl I get "Cannot cast android.app.ContextImpl to Activity". Is it clear? Any help?</p>
  
  <p>Thanks.</p>
  
  <pre><code>public static Context setLocale(Context context, String language) {
  
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
  
          Resources res = context.getResources();
          Configuration config = new Configuration(res.getConfiguration());
          if (Build.VERSION.SDK_INT &gt;= 17) {
              config.setLocale(locale);
              context = context.createConfigurationContext(config);
          } else {
              config.locale = locale;
              res.updateConfiguration(config, res.getDisplayMetrics());
          }
  
          return context;
      }
  </code></pre>
  
  <p>Edit 1: One good approach.</p>
  
  <p>If I insert the next piece of code in my SplashScreen activity:</p>
  
  <pre><code>Locale locale = new Locale("en");
  Locale.setDefault(locale);
  Configuration config = getBaseContext().getResources().getConfiguration();
  config.locale = locale;
  getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());
  </code></pre>
  
  <p>Then the locale works alongside the whole app but I know <strong>UpdateConfiguration is deprecated</strong> and I'm trying to use the new format that could be:</p>
  
  <pre><code>public static Context updateResources(Context context, String language) {
  
          Locale locale = new Locale(language);
          Locale.setDefault(locale);
  
          Resources res = context.getResources();
          Configuration config = new Configuration(res.getConfiguration());
          if (Build.VERSION.SDK_INT &lt;= 17) {
              config.setLocale(locale);
              context = context.createConfigurationContext(config);
          } else {
              config.locale = locale;
              res.updateConfiguration(config, res.getDisplayMetrics());
          }
  
          return context;
      }
  </code></pre>
  
  <p>But with this second updated approach it is not working, any suggestions?</p>