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.