2017-03-15 63 views
0

我有一个(或多或少)用其他语言翻译的应用程序。为此,我使用了locale并保存用户偏好的语言,我使用了SharedPreferences更改语言环境``onResume`方法后不工作?

使用此设置,我设法在应用程序内的任何/每个活动上加载首选语言。

 @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pick); 

    findViewById(R.id.flagcollection).setVisibility(View.GONE); 

    findViewById(R.id.worldmapswitch) 
      .setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        if (flagWorld) { 
         // means true 
         findViewById(R.id.flagcollection).setVisibility(View.GONE); 
         flagWorld = false; 
        } else { 
         findViewById(R.id.flagcollection).setVisibility(View.VISIBLE); 
         flagWorld = true; 
        } 
       } 
      }); 

    preferences = getSharedPreferences("settings_values", MODE_PRIVATE); 

    String SavedLocaleLang = preferences.getString("LocalLang", null); 
    Locale locale = new Locale(SavedLocaleLang); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 
.... 

的问题是,如果出于某种原因,我离开应用程序(不退出/结束活动),一段时间后,当我返回到应用程序,它运行使用手机的默认语言而不是保存的值语言环境。

然后,阅读有关的onResume的奇迹无数的职位,我的Java结合后无技能我尝试这样做:

@Override 
public void onResume(){ 
    super.onResume(); 
    // put your code here... 

    preferences = getSharedPreferences("settings_values", MODE_PRIVATE); 

    String SavedLocaleLang = preferences.getString("LocalLang", null); 
    Locale locale = new Locale(SavedLocaleLang); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 


} 

我以为我已经解决了我的问题,但在更短的测试结果后,不到1小时,我意识到我什么都没做(至少没有什么好处)。我在某个地方做错了什么。

如果任何人有技能,耐心和很多善意,请帮助我了解我做错了什么和在哪里。

任何帮助将不胜感激!请不要讽刺。在那里,我做得很好:P

谢谢!

P.S.网友“Cochi”要求的代码保存在Prefereces语言环境......所以在这里你去:

findViewById(R.id.flaghungary) 
      .setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String localeHU = "hu"; 
        SharedPreferences settings = getSharedPreferences("settings_values", MODE_PRIVATE); 
        SharedPreferences.Editor editor = settings.edit(); 
        editor.putString("LocalLang", localeHU); 
        editor.apply(); 

        Locale locale = new Locale("hu"); 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
        getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 
        setContentView(R.layout.activity_pick); 
        Context context = getApplicationContext(); 
        Toast.makeText(context, "Magyar nyelv kiválasztva", Toast.LENGTH_SHORT).show(); 
        recreate(); 
       } 


      }); 
+0

最有可能的,你的进程在你的应用在后台时被终止。 [这是非常正常的](https://developer.android.com/guide/components/processes-and-threads.html#Lifecycle)。除此之外,请解释“我做错某个地方”的含义。 – CommonsWare

+0

我最初的想法是,在保存区域设置时,是否在'SharedPreferences.Editor'对象上调用'commit()'或'apply()'?这是一个常见的疏忽,sharedPreferences不会被更新,除非你这样做。 – DeeV

+0

你好。感谢您的快速答复。我已经更新了onCreate的代码,就像以前一样。现在我正在尝试Nika Kurdadze的解决方案。也许它的工作原理:D – Robert

回答

1

尝试把码内部onCreate,在setContentView前:

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    String SavedLocaleLang = preferences.getString("LocalLang", null); 
    Locale locale = new Locale(SavedLocaleLang); 
    Locale.setDefault(locale); 
    Configuration config = new Configuration(); 
    config.locale = locale; 
    getResources().updateConfiguration(config, getResources().getDisplayMetrics()); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 
+0

你好。谢谢,但是,我应该从onCreate方法中复制更多的内容,以便您获得更大的图片。我已经做到了。现在我会试试你的方式:)。感谢您的建议,我需要一段时间才能确定它的正常工作,但请确保我留下回复。再次感谢您 – Robert

+0

再次。看来你的解决方案有效。或者至少我希望它:)我整天测试应用程序,没有发现任何故障。但:)。所以谢谢 ! – Robert

+0

我很高兴解决了这个问题。 –

相关问题