Multi-Language support and SpinnerPreference – Part II

As mentioned in Part I, calling updateConfiguration with the correct parameters for the locale provides a simple way to switch the language used in an app. Of course, the app should always start with the language last selected by the user and should provide an easy method with which to change the displayed language.

Saving state in an Android app can take many forms. For short term, such as handling orientation changes, using savedInstanceState in onCreate or a Headless Retained Fragment works very well. However these methods are not appropriate for user settings which require a persistent storage that spans the app restarting not just the activity. For this type of storage in Android there is a built-in database, SQLite, the file system, or SharedPreferences, which I ended up selecting since I was going to use a PreferenceFragment for my user settings interface.

To implement a PreferenceFragment create a SettingsActivity which extends AppCompatActivity and include the innerClass – MyPreferenceFragment which extends the PreferenceFragment and loads the xml file that defines the preferences.