The easy way is to register a BroadcastReceiver for Intent.ACTION_LOCALE_CHANGED.
Example: change your ViewModel data when language/locale changed.
public class QASViewModel extends ViewModel { @SuppressLint("StaticFieldLeak") private final FragmentActivity activity; public QASViewModel(@NonNull FragmentActivity activity) { this.activity = activity; setLangReceiver(); } private void setLangReceiver() { final QASViewModel qasViewModel = this; final BroadcastReceiver langReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // do action when language change } }; activity.getApplicationContext().registerReceiver(langReceiver, new IntentFilter(Intent.ACTION_LOCALE_CHANGED)); } }
No comments:
Post a Comment