Why might Resources.getString() intermittently return strings from the wrong locale?

[亡魂溺海] 提交于 2019-12-02 19:09:37

This is just a theory but you could be leaking a Context. Basically, the old activity might be reporting the string values rather than the newly created on.

A way to test this is:

  1. Change TAG to a member variable (NOT STATIC!).
  2. OnCreate, set TAG = this.toString(), this will put the memory address of the activity as the tag.
  3. Have the activity print out stuff using the initial locale.
  4. Do whatever to change the locale. This should (never verified this) restart the activity and you get a new activity. If you do. THen look at the log and see if the memory address is changing for the tag. If the memory address is the same as before the context was leaked.

Are you changing Locale while the application is running? If so, have you properly implemented the various elements of the Activity lifecycle (including onSaveInstanceState() and onRestoreInstanceState())?

According to http://developer.android.com/guide/topics/resources/runtime-changes.html, a change in configuration at runtime should cause the Activity to be destroyed & restarted. It sounds like your app is noticing the new configuration but is not restarting properly (until restarting the whole App, or changing orientation).

Are you doing anything funky in onSaveInstanceState or onDestroy?

P.s. If it corrects itself only on certain orientation changes, can you advise if those orientation changes occur on layouts that have different vertical & horizontal layout files?

I faced the same issue.

For me, I was trying to get the string (in the onCreate()) before importing language settings from shared preferences (done in the onResume()).

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