Android 是否使用SharedReferences更改启动活动中的应用程序语言?

Android 是否使用SharedReferences更改启动活动中的应用程序语言?,android,Android,我使用以下代码更改语言: public void setLocale(String languageToLoad){ Locale locale = new Locale(languageToLoad); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getResources().updateConfigurati

我使用以下代码更改语言:

public void setLocale(String languageToLoad){
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}
我的问题是,当我尝试使用SharedReferences加载“locale”时,由于刷新活动,它似乎陷入了一个循环中。那么,如何在“开始”活动中加载最后一个位置。
提前感谢。

您无需再次启动活动。您只需通过调用setContentView()重新设置视图和布局即可。

无需再次启动活动。您只需通过调用setContentView()重新定义视图和布局即可。

我找到了答案:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //some code here
    if (last_local.equals("fa") && current_local.equals("en"))
        setLocale("fa");
    //some code here
}

 public void setLocale(String languageToLoad){
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    SharedPreferences Settings=getSharedPreferences("Last Setting",1);
    Settings.edit().putString("Locale Status", languageToLoad).commit();
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}
我的问题是在onstop()方法中,我保存了loace,这是循环的原因。

我找到了答案:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //some code here
    if (last_local.equals("fa") && current_local.equals("en"))
        setLocale("fa");
    //some code here
}

 public void setLocale(String languageToLoad){
    Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    SharedPreferences Settings=getSharedPreferences("Last Setting",1);
    Settings.edit().putString("Locale Status", languageToLoad).commit();
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

我的问题是在onstop()方法中,我保存了loace,这是循环的原因。

调用setLocale的代码在哪里?能否将要设置的区域设置与设置的区域设置进行比较?如果它们相同,则什么也不做?调用setLocale的代码在哪里?能否将要设置的区域设置与已设置的区域设置进行比较?如果他们是一样的,什么也不做?