I'm developing an application for android and when everything should be finished I've found an extrange behaviour. In some devices the screen starts to flicker, and there is no apparent reason for that. It may happen in initial splash with only an AsyncTask quering a webservice or in the home screen with no asynctask at all.
It's strange because only happens in devices with android 4.2, nor in 2.3 or 2.2. I've tried enabling the tag harware-accelration in manifest but I've no clue for what can be the reason
Some help?
Thanks in advance.
Finally I've managed how to avoid this. Here was the trick, just changing my Application's Override OnConfigurationChanged.
This was my old code
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Locale l = getLocale();
Configuration config = new Configuration(newConfig); // get Modifiable Config from actual config changed
config.locale = l;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
and I've changed to this
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Locale l = getLocale();
Configuration config = new Configuration(newConfig); // get Modifiable Config from actual config changed
config.locale = l;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
I hope someone finds this useful.
Related
I am working with google map in my activity.
When i change language for example Chinese in my app settings
the langauge of map did not change till i reopen my app.
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.activity_maps);
i searched a lot about this issue i found that it is google map caching issue but did not found any solution.
I can think about 2 options to solve this:
Calling setContentView in the onConfigurationChanged method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (language_changed){//pseudo_code
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.activity_maps);
//now you need to do again all the findViewById call
}
}
Restart the activity in the onConfigurationChanged method
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (language_changed){//pseudo_code
startActivity(CurrentActivity.this, CurrentActivity.class);
finish();
}
}
I've not tested this, but maybe it's a starting point to try to solve your problem.
Don't forget to add android:configChanges="locale" to your activity tag in the AndroidManifest.xml to let the system know that you want to handle Locale changes by yourself. Read more here if needed
You can use BroadcastReceiver with LOCALE_CHANGED Intent to detect setting change, and then update your view (Map view or total layout).
https://developer.android.com/reference/android/content/Intent.html#ACTION_LOCALE_CHANGED
public class LocaleReceiver extends BroadcastReceiver {
public LocaleReceiver() {}
#Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())){
// Call a method to trigger view refreshing here
}
}
}
I have an app where the user can change its language.
Everything is working fine, with just this code on my MainActivity.onCreate():
String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", "default");
Configuration config = getResources().getConfiguration();
if( lang.equals("default") ) lang = Locale.getDefault().getLanguage();
config.locale = new Locale(lang);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
When I restart the app or navigate through activities it's still in the right language.
The only problem is on the PreferenceActivity screen. When the orientation changes, the PreferenceActivity title (and only it) changes to the default device language.
The prefs are still checked correctly, if I go back (closing the PreferenceActivity) the app is still on the right language, but the PreferenceActivity stays wrong until I restart the app.
I tried forcing the code above on the PreferenceActivity.onCreate() and altough debugging seems OK, the PrefenceActivity Title stays wrong.
Here's my PrefenceActivity code:
public class PreferencesActivity extends PreferenceActivity {
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
This behavior doesn't happen on any other Activity :/
Locking the screen orientation is not an option.
Any thoughts?
Thanks.
OK, this fixed it for me.
#Override
protected void onSaveInstanceState(Bundle outState) {
String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", "default");
Configuration config = getResources().getConfiguration();
if( lang.equals("default") ) lang = Locale.getDefault().getLanguage();
config.locale = new Locale(lang);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
super.onSaveInstanceState(outState);
}
I solved this problem using onConfigurationChanged().
In this method I'm saving preferred language again in Configuration object.
OnCreate method of activity is called when orientation is changes, so some property is again set by your code, look at your on oncreate method or lock your screen orientation.
You can lock your screen orientation to your activity by Using
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
I have implemented custom locale selection about a year ago but after 4.1 release users start to complain on constant activity blinking. Here is code I'm using (compiled from different SO answers):
public final class TestApplication extends Application
{
private Locale desiredLocale = new Locale("ru-RU");
#Override
public void onCreate() {
super.onCreate();
updateLocale(new Configuration());
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
updateLocale(newConfig);
}
private void updateLocale(Configuration newConfig) {
newConfig.locale = desiredLocale;
Locale.setDefault(desiredLocale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
Application contains only one empty activity, which is recreated by Android every second after I change device orientation. Here is the source of sample.
It looks like all applications which use this technique became invalid. What is the correct approach?
This line caused solution to fail:
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
Correct (at least it works) implementation is defined here
https://stackoverflow.com/a/14010044/554336 :
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
So new configuration instance should be created every time.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Changing Locale within the app itself
in my application I need to "force" language let's say that I have locale in english as default polish and finnish, according to that post I had created function posted also bellow, function is called in createActivity(), but the problem is it does not work.... any idea why? Any suggestions?
private void setLocale(String localeCode){
Locale locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
Here is what I got so far. I know this question is resolved, but my solution is easier and more compact. No other changes are needed, no android:configurationChanges attribute for all activities in your manifest.
public class SigmaMiddleEastApplication extends PPGApplication {
#Override
public void onConfigurationChanged(Configuration newConfig) {
newConfig.locale = Locale.ENGLISH;
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
}
BEWARE this may cause problems:
What could cause an Android activity to relaunch itself infinitely when returning from camera?
Add the following in the manifest (for every activity) :
android:configChanges="locale"
I want to rotate my phone and keep the locale. I have folders values, values-en and values-hr. In every activity I have
android:configChanges="keyboardHidden|orientation|locale"
and in every activity.java file I have
private Locale locale = null;
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
and in onCreate
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString("language","en");
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
It works till I rotate my phone - app is still working, buttons and everything is fine - but locale is getting weird - sometimes it's my locale (hr) sometimes it's (en)...
What else I need to add?
I simply removed "if's" and everything is working now. Yes, it is a little "bulky" every time to "re-force" a locale but I couldn't find another way.
My solution is a little bit different, I just "re-force" it on onCreate and onConfigChange - not every 100ms or so and on Desire (2.2) it's working flawlessly.
#Austyn
Yes, it's pretty much the same question but I though is there another solution except "100ms forcing".
#Mayra
Users wanna have their mother-tongue (HR) but it's not supported in (current) mobile phones so this is the only way - to force it =/