Reputation: 5462
Here is the code of my onActivityResult
method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
String contents = data.getStringExtra("SCAN_RESULT");
if (contents.length() == 26) {
BillBarcode barcode = new BillBarcode(contents);
edtBillId.setText(barcode.extract(BillBarcode.BarcodePart.BillId));
edtPaymentId.setText(barcode.extract(BillBarcode.BarcodePart.PaymentId));
launchService();
} else {
Dialog dialog = new DialogBuilder()
.setTitle(getString(R.string.dialog_title_global_error))
.setMessage(getString(R.string.unknown_barcode))
.build(getActivity());
dialog.show();
}
}
}
The problem is that getString(R.string.dialog_title_global_error)
and getString(R.string.unknown_barcode)
always returns english value while I have Farsi value too and the locale is farsi too.
The problem only exist in this method.
Farsi value:
<string name="unknown_barcode">بارکد قابل استفاده نیست.</string>
English value:
<string name="unknown_barcode">Unknown barcode</string>
EDIT
I have a setting page and set my locale when user selects persian from language page by this code:
String languageToLoad = "fa";
Resources res = context.getResources();
// Change locale settings in the app.
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(languageToLoad);
Upvotes: 3
Views: 1592
Reputation: 15398
Let me try to round up all the comments in an answer. Your problem is a combination of two implementation mistakes:
You are setting the locale of the current Activity
s context programmatically. However you are doing so in an unsupported way that may yield incorrect results.
When your activity gets a result from another activity in OnActivityResult()
, your Activity
either gets restarted completely or the context configuration gets reset to the system's default locale. Either way, the locale you set up in the settings dialog is lost.
Solutions
In particular, while just changing the locale in the configuration class may work for you, it is clearly not the correct way to change an application's locale. Doing it properly requires a little more work:
Locale locale; // set to locale of your choice, i.e. "fa"
Configuration config = getResources().getConfiguration();
config.setLocale(locale); // There's a setter, don't set it directly
getApplicationContext().getResources().updateConfiguration(
config,
getApplicationContext().getResources().getDisplayMetrics()
);
// you might even need to use getApplicationContext().getBaseContext(), here.
Locale.setLocale(locale);
Setting the locale on the application context should survive the restart of an activity, but as per Android's lifecycle guarantees, you should not assume that the locale stays around.
SharedPreference
) and obtain and re-set it when the app or your activity is restarted (i.e. at least in OnCreate()
). Remember that Android is free to save and restart your activities at any time outside of your control and it is your responsibility to handle the restart gracefully.Upvotes: 1