1

When my app starts up for the first time, I want to detect the user's current locale and set a SharedPreference accordingly. I'm grabbing the user's current locale from getResources(), which I've come to understand is set when the app starts for the first time:

// Set the locale
Locale locale = getResources().getConfiguration().locale;
if(locale != Locale.US)
{
    Log.i("ActivityDownloader", "Locale: " + locale.toString() + ", (" + Locale.US.toString() + ")");
}

Unfortunately, I'm seeing that log entry in the console with the following text:

I/ActivityDownloader: Locale: en_US, (en_US)

Why don't the two Locale match? Should I be matching Locale.toString() instead?

IAmKale
  • 3,146
  • 1
  • 25
  • 46

1 Answers1

1

You need to use the equals() method. The == and != operators won't work for this.

Locale locale = getResources().getConfiguration().locale;
if(!locale.equals(Locale.US))
{
    Log.i("ActivityDownloader", "Locale: " + locale.toString() + ", (" + Locale.US.toString() + ")");
}

I believe this is because == and != will compare references rather than the value. See https://stackoverflow.com/a/767379/935779

Community
  • 1
  • 1
Kirk
  • 16,182
  • 20
  • 80
  • 112