Search code examples
javaandroidandroid-activitylocale

Stop android activity from being destroyed on locale change


I have an android app that handles locale changes on its own. When I leave the app and change the locale "Application->onConfigurationChanged" is called (NOT Activity->onConfigurationChanged) and when I resume the app the activity is destroyed and restarted.

I have already placed android:configChanges="orientation|keyboardHidden|screenSize|locale" in the activity section in the manifest.

How can I stop my activity from being recreated on resume after a locale change?


Solution

  • The default reaction to configuration changes in Android is to destroy the activity. To avoid this happening for locale changes, we had the "locale" configuration change registered in the default AndroidManifest.xml, however, you also need to register that you are handling the layoutDirection change, otherwise Android will not call onConfigurationChange() for language changes, but tear down the activity instead.

    Adding android:configChanges="layoutDirection|locale" prevents the activity from being destroyed on resume.

    Source