onConfigurationChange not called after changing locale

為{幸葍}努か 提交于 2019-12-18 14:58:32

问题


I have to refresh the fragments contents on change of language from one fragment. So I thought of using onConfigurationChange method which is in my Main activity (this activity controls all the fragments) and recreate all the fragments on change of locale. But this method is not being called on change of locale.

I have included locale under the activity tag of manifest file. onConfigurationChange is being called on change of orientation.

i am changing language as given in this link Change language programmatically in Android and its working.

Can someone please clarify what change would be required to fix this issue.


回答1:


For others who encounter this issue the OP was right but a bit unclear.
You have to define android:configChanges="layoutDirection|locale" in order for onConfigurationChanged() to be called.
I assume that this is necessary because locale change might also affect layout direction (for RTL languages) so declaring just locale might not be enough, yet this is only my assumption on this matter.




回答2:


user layoutDirection attribute in manifest




回答3:


You can extend from Application and set it in the manifest. Then, put onConfigurationChanged there, and check if the previous locale is the same as the current one:

if(!_currentLocale.equals(newConfig.locale))
  {
  _currentLocale=newConfig.locale;
  // locale has changed
  }

The "_currentLocale" variable should be initialized on the onCreate method of the new class (that extends Application), as such:

  @Override
  public void onCreate()
    {
    super.onCreate();
    _currentLocale=getResources().getConfiguration().locale;
    }


来源:https://stackoverflow.com/questions/18725396/onconfigurationchange-not-called-after-changing-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!