开发者

TabActivity won't change language

I have problem. I have TabActivity with intent tabs. In my application user can change language in preference settings. When user change language and application go back to my TabActivity doing this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case 1:
        finish();
        startActivity(getIntent());
        break;

    default:
        break;
    }
}

This code run perfectly, because all text is in changed language.

Problem occur, when i restart application. Some texts are not in proper language (are in system default). When i open preference screen once again and back to my TabActivity, texts are all translated.

How can i translate all texts after restart application? Why when i first run application not all texts are in proper language?

Im sorry for my English, i hope u understand what i mean and help me. Thank you.

This is code from preferenceActivity when saving:

    String lang = preferences.getString("Language", "");
    Configuration config = new Configuration();
    if (!TextUtils.isEmpty(lang))
          config.locale = new Locale(lang);
        else
          config.locale = Locale.getDefault();
    Locale.setDefault(new Locale(lang));
    getBaseContext().getResources().updateConfiguration(config, null);

tabActivity:

public class PlanActivity extends TabActivity {

SharedPreferences preferences;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.app_name);

    preferences = getSharedPreferences(Constans.PREF, Activity.MODE_PRIVATE);
    edytor = preferences.edit();

    String lang = preference开发者_运维百科s.getString("Language", "en");
    Configuration config = new Configuration();
    if (!TextUtils.isEmpty(lang))
        config.locale = new Locale(lang);
    else
        config.locale = Locale.getDefault();

    Locale.setDefault(new Locale(lang));
    getBaseContext().getResources().updateConfiguration(config, null);

     setContentView(R.layout.main);
        TabHost tabHost = getTabHost();
......... more

And this code is not working properly. I have to go to settings and go back to tabActivity for refresh texts


Are you correctly saving the changes to the SharedPreferences object? The code you provide does not show you using a SharedPreferences.Editor to save your changes.

EDIT: ensure that the preference file you're saving to in your preference Activity is the same one you're opening in your Tab activity (preferences = getSharedPreferences(Constans.PREF, Activity.MODE_PRIVATE);). If your tab activity does not find the preferences you're sure you saved in the preference activity then either your save code is not being called (either use a break point or a log statement to verify) or you are saving to the wrong file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜