更改整个应用程序中不使用Handler的语言

时间:2015-10-22 07:48:48

标签: android android-intent handler splash multiple-languages

我有SplashActivityMainActivity,默认设备语言是英语。在SplashActivity中,我将应用程序的默认语言更改为vi(越南语)。我正在使用2种方式从SplashActivity转移到我在下面的代码中显示的MainActivity 我已经非常仔细地测试了很多次,但我仍然不知道这个问题 任何帮助将不胜感激

public class SplashActivity extends Activity {
    ...
    protected void onCreate(Bundle savedInstanceState) {
        ...
        changeDefaultLanguageConfigLanguage("vi");
        moveToMain();
        ...
    }
    private void changeDefaultLanguageConfigLanguage(String language) {
        Configuration config = new Configuration();
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        config.locale = locale;

        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

    public void moveToMain() {

        /*
           Way 1: The language in MainActivity change
        */  
        Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
        context.startActivity(mainIntent);

        /*
           Way 2: The language in MainActivity don't change
        */ 

        new Handler().postDelayed(new Runnable() {@
            Override
            public void run() {


                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(mainIntent);

                //finish();

                //overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        }, 3000);
    }
}

的AndroidManifest.xml

         <activity
            android:name=".SplashActivity"
            android:label="@string/title_activity_splash"
            android:configChanges="orientation|keyboardHidden" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/title_activity_main" >
        </activity>

1 个答案:

答案 0 :(得分:1)

如果我没记错,您必须重新创建活动才能使配置更改生效。您可以使用Activity.recreate()(在API 11中引入)。

我还没有尝试过,但为了避免重新创建活动,我会尝试在活动的locale属性中加入configChanges