Android-локаль пользователя всегда сбрасывается после onCreate? - PullRequest
0 голосов
/ 14 сентября 2010

Я хочу, чтобы в моем приложении были настраиваемые языковые параметры.

Итак, в onCreate своей деятельности я вызываю Resources.updateConfiguration с новым языковым стандартом.

Однако после onCreate (в какой-то момент я не могу найти его, когда), языковой стандарт устанавливается обратно на языковой стандарт по умолчанию.

В приведенном ниже примере кода строки, показанные в основном макете (как завышено с помощью setContentView), показываютверсия на языке «in», НО, когда я нажимаю кнопку меню, при которой вызывается onCreateMenu, строки берутся из «en» (по умолчанию) локали.

В журнале это показано:

 18337               oncreate  D  { scale=1.0 imsi=525/1 loc=intouch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=143}
 30430        ActivityManager  I  Displayed activity yuku.coba.locale/.CobaLocaleActivity: 266 ms (total 266 ms)
 18337        KeyCharacterMap  W  No keyboard for id 65540
 18337        KeyCharacterMap  W  Using default keymap: /system/usr/keychars/qwerty.kcm.bin
 18337                 onmenu  D  { scale=1.0 imsi=525/1 loc=en_GB touch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=143}

Между "oncreate" и "onmenu", язык волшебным образом меняется.

Пожалуйста, помогите, я безуспешно возился с этим.

Фрагмент кода:

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       Configuration c = new Configuration();
       c.locale = new Locale("in");
       getResources().updateConfiguration(c, null);

       setContentView(R.layout.main);
       Log.d("oncreate", getResources().getConfiguration().toString());
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       Log.d("onmenu", getResources().getConfiguration().toString());
       new MenuInflater(this).inflate(R.menu.utama, menu);

       return true;
   }

Ответы [ 4 ]

1 голос
/ 19 сентября 2010

Каждые 100 мс сбрасывают конфигурацию:)

0 голосов
/ 12 февраля 2014

Вы можете использовать метод, который я написал, чтобы справиться с этой ситуацией:

/**
     * Sets app language locale also taking account system resets of the Locale; Resetting of the Locale is done right after onResume of the first Activity that is run in the application.
     * So... if the AppLanugage is set e.g. in first activitys onCreate method, this language will be reset shortly after; To counter this the method starts the Timer (if a flag is set) to
     * handle pottential Locale resets that are done by the system.
     * 
     * In case the flag alsoReloadAfterDelay is set, usually the numberOfResetsOsDoes parameter and the numberOfResetsOsDoes should be used separately from eachother; Either the timer
     * shoudl run till the numberOfResetsOsDoes is matched, or run till the timer runs out of time, irrespectively of the number of resets the os does
     * @param appContext    The application context
     * @param lang  New language to set the locale
     * @param alsoReloadAfterDelay  The flag that says whether to start a timer that will handle pottential Locale resets, that are done by Android OS;
     * @param numberOfResetsOsDoes  The number of resets the OS does after onResume method of the first activity; So far I noticed that it is happening twice (emulator 2.3.3)
     * @param resetTimerDuration    The duration of the reset timer;
     * @see <a href="https://groups.google.com/forum/?hl=en#!topic/android-developers/VEAWMCdyIWg">https://groups.google.com/forum/?hl=en#!topic/android-developers/VEAWMCdyIWg</a> 
     * @return  True if the operation succeded (if the given lang param was appliable for instance); False otherwise
     */
    public static boolean setAppLanguageLocale(final Context appContext, final String lang, boolean alsoReloadAfterDelay, final int numberOfResetsOsDoes, int resetTimerDuration)
    {       
        final String previousAppLanguage = getAppLanguage(appContext);
        final String newLang = lang.toUpperCase(Locale.US);

        if(previousAppLanguage.equals(newLang))
            return true;

        setAppLanguageLocaleP(appContext, lang);

        if(alsoReloadAfterDelay)
        {               
            new CancellableCountDownTimer(resetTimerDuration, 10)
            {
                private int aResetCounter = 0;

                @Override
                public void onTick(long millisUntilFinished)
                {
                    if(aResetCounter == numberOfResetsOsDoes)
                    {
                        this.cancel();
                        return;
                    }

                    String currentAppLanguage = getAppLanguage(appContext);

                    if(!currentAppLanguage.equals(newLang))
                    {
                        aResetCounter++;

                        setAppLanguageLocale(appContext, lang);
                    }
                }

                @Override
                public void onFinish()
                {                   
                }
            }.start();
        }

        return true;
    }
0 голосов
/ 18 ноября 2010

вместо getResources (). UpdateConfiguration (c, null);

try getBaseContext (). GetResources (). UpdateConfiguration (c, null);

(не getDefaultContext извините)

0 голосов
/ 14 сентября 2010

Советую не форсировать локаль чем-то отличным от настроек устройства. Вы столкнетесь с проблемами. Вы можете переместить ваш звонок на onResume. Будет лучше, но все равно ты будешь бороться с конфигурацией устройства. Снова. Не делай этого.

...