Note that there are some explanatory texts on larger screens.

plurals
  1. POChange language on runtime in Android
    primarykey
    data
    text
    <p>I have to change the language on runtime in Android (yes, I know that this is not a good behaviour, but this is a requirement...).</p> <p>So I have a basic class, from which every activity extends. This class has the following function:</p> <pre><code>public static void changeLanguage(Context context) { Resources res = context.getResources(); /* * Change locale settings in the app. */ DisplayMetrics dm = res.getDisplayMetrics(); /* * Store and load data in this preferences */ android.content.res.Configuration conf = res.getConfiguration(); String[] localArray = res.getStringArray(R.array.language_short_array); if (localArray != null) { SharedPreferences settings = context.getSharedPreferences( MyService.APP_ID, MODE_PRIVATE); conf.locale = new Locale(localArray[settings.getInt( PREFERED_LANGUAGE_KEY, 0)]); res.updateConfiguration(conf, dm); } } </code></pre> <p>I will call this method in onCreate:</p> <pre><code>@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); changeLanguage(this); } </code></pre> <p>This is in the super-class. My Activities extends from it and call super.onCreate at first. After this call, they set their layout and initializes their settings...</p> <p>I thought that my lines of code would make it. But I have the following problem: Sometimes, the activity changes the language and sometimes not!</p> <p>If I set a debug breakpoint on it and after the programm pauses I press continue, everything works fine. So I think, in some cases, where my Application is "slow enough", the language will change correctly, whereas the language won't change if the application is too fast...</p> <p>Is there any solution of my problem? How can I be sure, that my language will change correctly in any time?</p> <p>Thanks a lot!</p> <p>Edit: Here is an example for a class which extends from my super-class</p> <pre><code>public class MainMenuActivity extends BaseActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload