0

I have a BroadcastReceiver that listens to a locale change. Here's my problem:

I navigate to an Activity and then want to change locale (language setting) which I do by going to the settings app. The BroadcastReceiver then listens in onReceive() once a change is made. I then navigate back to the app and when I do so I'd like to take a user to another Activity.

Also, a locale modification corresponds to a change in configuration which means an Activity will be destroyed and created again. https://developer.android.com/guide/topics/resources/runtime-changes.html

Here is the BroadcastReceiver:

public class LocaleReceiver extends BroadcastReceiver {
    public LocaleReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.

        if(Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())){
            MainActivity.isLocaleChanged = true;
        }
    }
}

And here is the Activity that uses the static variable set by the BroadcastReceiver.

public class MainActivity extends AppCompatActivity {

    public static boolean isLocaleChanged = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(isLocaleChanged){
            Intent intent = new Intent(this,SecondActivity.class);
            startActivity(intent);
            isLocaleChanged = false;
        }
    }
}

And indeed I am able to navigate to a different Activity !

However, I'd like to do this in a manner that does not use static variables (since they are evil :(). Is there any other way to accomplish this.

I'd also be extra happy if there were no SharedPreferences involved.

user1841702
  • 2,683
  • 6
  • 35
  • 53
  • i have some questions : 1. when `MainActivity.isLocaleChanged = true;` is done is your mainactivity open in foreground (visible to user) ? 2. when the Locale is changed you want to redirect the user to `SecondActivity` only ?? – Janki Gadhiya Jul 07 '16 at 07:26
  • 1. No, its done while it (and the entire app) is in the background. I'd be in the settings app while onReceive() executes. 2. Yes. – user1841702 Jul 07 '16 at 07:31
  • then you have only one option `SharedPreference` as Gabe Sechan is saying in below answer..!! – Janki Gadhiya Jul 07 '16 at 07:37

1 Answers1

2

Well, you can turn off configuration changes for locale, implement onConfigurationChanged, check if the change is to the locale, and launch the new activity there. I'm not sure if I'd suggest it though, you'll have issues when you return with strings. This is a case where you have to store state non-locally- either in a static, on disk (sharedPreference) or via a state singleton, or some other means. It isn't that statics are evil, its that they can be misused. This is a case where they make sense.

I'd actually recommend static over shared preference here, as shared preferences may stick around if you don't clear it properly and screw up a later execution of your app. A static won't, it will be cleared when your app is killed worst case.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for this. I did try using onConfigurationChanged() but I can't seem to make it register any callback. What I did was add android:configChanges="locale" to the Activity I want to register it with in the manifest and also included onConfigurationChanged() in MainActivity. But it just doesn't seem to register a locale change. And what is 'turn off configuration change for locale' - looks like I've missed something. – user1841702 Jul 07 '16 at 07:36
  • Woah! it was actually android:configChanges="layoutDirection|locale" http://stackoverflow.com/questions/18725396/onconfigurationchange-not-called-after-changing-locale to get onConfigChanged() to work. Thanks a million though. I also see why this is not recommended. Thanks again. – user1841702 Jul 07 '16 at 07:47