Why does the name of the Fragment not change when changing the language?

---------- Class Application

public class BaseApp extends Application{ public static Context context; @Override public void onCreate() { super.onCreate(); context=getApplicationContext(); } } 

---------- Class Constans are simple

  public final class Constants { // Screen titles public static final String LOGIN_FRAGMENT_TAG = BaseApp.context.getString(R.string.const_login); public static final String MY_LEARNING_FRAGMENT_TAG = BaseApp.context.getString(R.string.const_my_learning); } 

---------- A function that changes the language.

  Locale myLocale = new Locale(lang); Configuration configuration = BaseApp.context.getResources().getConfiguration(); configuration.setLayoutDirection(myLocale); getResources().updateConfiguration(configuration, getResources().getDisplayMetrics()); Intent goToMainActivity = new Intent(getActivity(), LoginActivity.class); startActivity(goToMainActivity); getActivity().finish(); 

In general, the whole thing works only after restarting the application. I understand how something is wrong with the class Constants. How can you make it work

  • not afraid to store content in static variable? - KolinLoures
  • @KolinLoures if this is the application context - not scary - Vladimir Parfenov

1 answer 1

Static constants are not re-initialized when changing language. Yes, and should not, they are constants. Everything will work fine if you set headers for fragments not through constants, but directly via BaseApp.context.getString(R.string.const_login) .

And when you restart, everything works, because the application is removed from memory and starts up again, which leads to the initialization of static fields afterwards.