我可以将我的应用程序更改为波斯语,但每当我关闭应用程序并重新打开它时,它都会恢复为默认的英语。另一个问题是当我点击它时英语不起作用,当我点击后退按钮时,它又回到之前的活动英语
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
final String lang = settings.getString("LANG", "");
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources()
.getDisplayMetrics());
}
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setPrompt("select language");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, languages);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2 ,long arg3 ) {
Configuration config = new Configuration();
Locale locale;
switch (arg2) {
case 0:
config.locale = Locale.ENGLISH;
break;
case 1:
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.edit().putString("LANG", "fa").apply();
config.locale = new Locale("fa");
startActivity(new Intent(getBaseContext(), MainActivity.class));
finish();
break;
}
getResources().updateConfiguration(config, null);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private String[] languages = { "English","فارسی" };
}