Search code examples
javaandroidmvvmconfigurationandroid-mvvm

Android how to Check if language has changed


I create an application with MVVM concept, there is fragment for viewpager in my Activity. some data changed when I change my language in my application, but the data that showed by webservices is not change. so I try to add android:configChanges="locale" in my every Activity and I already add this code on my Activity class :

  @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
       recreate();
    }
}

But its make my UI recreate every configuration change, including Screen Rotation while I just want to recreate if Language is changed.

this is my fragment code :

public class CatalogueFragment extends BaseFragment<FragmentCatalogueBinding, CatalogueViewModel>
    implements CatalogueNavigator, CatalogueAdapter.CatalogueAdapterListener {

@Inject
CatalogueAdapter adapter;

@Inject
LinearLayoutManager mLayoutManager;

@Inject
ViewModelProvider.Factory factory;

FragmentCatalogueBinding fragmentCatalogueBinding;

private CatalogueViewModel catalogueViewModel;

public static CatalogueFragment newInstance(int Pos) {
    Bundle args = new Bundle();
    CatalogueFragment fragment = new CatalogueFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public int getBindingVariable() {
    return BR.viewModel;
}

@Override
public int getLayoutId() {
    return R.layout.fragment_catalogue;
}

@Override
public CatalogueViewModel getViewModel() {
    catalogueViewModel = ViewModelProviders.of(this, factory).get(CatalogueViewModel.class);
    return catalogueViewModel;
}

@Override
public void handleError(String error) {
    // handle error
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    catalogueViewModel.setNavigator(this);
    adapter.setListener(this);
}

@Override
public void onRetryClick() {
    catalogueViewModel.fetchData();
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    fragmentCatalogueBinding = getViewDataBinding();
    setUp();
}

@Override
public void updateData(List<Movie> movieList) {
    adapter.addItems(movieList);
}

private void setUp() {
    mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    fragmentCatalogueBinding.recyclerCatalogue.setLayoutManager(mLayoutManager);
    fragmentCatalogueBinding.recyclerCatalogue.setItemAnimator(new DefaultItemAnimator());
    fragmentCatalogueBinding.recyclerCatalogue.setAdapter(adapter);
}
}

and this is my ViewModel class

 public class CatalogueViewModel extends BaseViewModel {

    private final MutableLiveData<List<Movie>> movieListLiveData;

    public CatalogueViewModel(DataManager dataManager, SchedulerProvider schedulerProvider) {
        super(dataManager, schedulerProvider);
        movieListLiveData = new MutableLiveData<>();
        fetchData();
    }

    public void fetchData() {
        setIsLoading(true);
        getCompositeDisposable().add(getDataManager()
                .getApiHelper().doMovieCall(URLConfig.API_KEY, getDataManager().getLanguage())
                .subscribeOn(getSchedulerProvider().io())
                .observeOn(getSchedulerProvider().ui())
                .subscribe(movieResponse -> {
                    if (movieResponse != null && movieResponse.getResults() != null) {
                        movieListLiveData.setValue(movieResponse.getResults());
                    }
                    setIsLoading(false);
                }, throwable -> {
                    setIsLoading(false);
//                    getNavigator().handleError(throwable);
                }));
    }

    public LiveData<List<Movie>> getMovieListLiveData() {
        return movieListLiveData;
    }
}

Can anybody show me where is my wrong? Thank you very much


Solution

  • You can use: ACTION_LOCALE_CHANGED

    Here an example:

    private BroadcastReceiver mLangReceiver = null;
    
    protected BroadcastReceiver setupLangReceiver(){
    
        if(mLangReceiver == null) {
    
            mLangReceiver = new BroadcastReceiver() {
    
                @Override
                public void onReceive(Context context, Intent intent) {
                    // do what you want
                }
    
            };
    
            registerReceiver(mLangReceiver, new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
        }
    
        return mLangReceiver;
    }