Reputation: 193
I have two activities:
-> Main Activity
-> Configuration Activity
From 'MainActivity' I open the 'ConfigurationActivity'. This 'ConfigurationActivity' is shown in horizontal screen for small devices.
After click on 'Save Button', I re-open the 'MainActivity'. Because the 'ConfigurationActivity' is shown in horizontal screen, the 'MainActivity' is also shown in horizontal screen.
If I flip the phone to vertical screen I lose my locale configuration.
This is my code to set locale:
Locale locale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(refresh);
finish();
I have tried to force, in Manifest, on 'MainActivity', the locale and orientation without success.
<activity
android:name="main.myproject.MainActivity"
android:label="@string/title_activity_mainactivity"
android:windowSoftInputMode="stateHidden"
android:configChanges="locale|orientation" >
</activity>
Could be the solution to save the locale into a preference, then on method Create() in 'MainActivity' call that preference?
Thanks.
Edit (1):
The code below doesn't work. It continues to, whenever I flip the screen from horizontal to vertical, it changes the application language by itself. I only have two languages, the mother one: Portuguese and the second one: English.
In 'ConfigurationActivity', I've changed my code to:
public void setLocale(String lang) {
Locale locale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
SharedPreferences sPrefs = getSharedPreferences("localePref", 0);
sPrefs.edit().putString("lang", lang).commit();
Intent refresh = new Intent(this, MainActivity.class);
refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(refresh);
finish();
}
And in the 'MainActivity' I've created a method to call the language.
private void load_language(){
SharedPreferences sPrefs = getSharedPreferences("localePref", 0);
String lang = sPrefs.getString("lang", "pt_PT");
Locale locale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
}
I 'override' the functions onCreate() and onConfigurationChanged():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_MainActivity);
load_language();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_MainActivity);
load_language();
}
I've 'log.e' the value from the preference and the values is right. What am I doing wrong?
Upvotes: 0
Views: 181
Reputation: 2348
An orientation-change re-builds the whole activity. This way all the Variables and their values get re-created aswell. So your idea to save it in local Preferences is probably the best solution here.
Saving:
SharedPreferences sPrefs = getSharedPreferences("myAppsPreferences", 0);
sPrefs.edit().putBoolean("myKeyHere", value).commit();
Loading:
SharedPreferences sPrefs = getSharedPreferences("myAppsPreferences", 0);
Boolean bCheck = sPrefs.getBoolean("MyKeyHere", defaultValue);
works for other primitive datatypes aswell!
Upvotes: 1