0

This code part of a method always causes the app to restart. It's not a crash, just a restart without any error messages. The curious thing, so far I have only discovered this behavior with Nokia 8 (Android 9) and Samsung Note 20 Ultra (Android 12).

The code is inside FragmentSettings, so it's the Settings menu item in the app. If the user makes any changes in it, the app will be restarted afterwards. With the smartphones mentioned above, the app is restarted directly as soon as the user wants to call up "Settings". So the user does not even have the opportunity to make changes.

I know this isn't a lot of code I'm showing here, but do you have any suspicions as to why this phenomenon is happening?

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                LocaleManager.setNewLocale(getContext(), countryToCode.get(spinner.getSelectedItem().toString()));

                requireActivity().finish();
                Intent i = new Intent(getActivity(), ActivityMain.class);
                i.putExtra("RELOAD_VALUES", true);
                startActivity(i);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {

            }

        });
ng-User
  • 243
  • 1
  • 6
  • 18

1 Answers1

0

here is an example of how you change language in java

private Button langselect;

inside oncreate()

langselect = findViewById(R.id.selectlang);
langselect.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showChangeLanguageDialog();
    }
});

outside on create()

private void showChangeLanguageDialog(){

    final String[] listItems = {"English","French"};
    final AlertDialog.Builder mBuilder = new AlertDialog.Builder(LoginActivity.this);
    mBuilder.setTitle("Choose Language");
    mBuilder.setSingleChoiceItems(listItems, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int i) {

            if (i == 0 ){
                setLocale("default");
                recreate();
            }
            else if (i == 1){
                setLocale("fr");
                recreate();
            }
            dialog.dismiss();
        }

    });

    AlertDialog mDialog = mBuilder.create();
    mDialog.show();
}
private void setLocale(String lang) {

    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration configuration = new Configuration();
    configuration.locale = locale;
    getBaseContext().getResources().updateConfiguration(configuration, getBaseContext().getResources().getDisplayMetrics());
    SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
    editor.putString("MY_LANG",lang);
    editor.apply();

}
Taa Lee
  • 66
  • 6
  • Hi @Taa-Lee, still same phenomenon. ```startActivity(i); and requireActivity().finish();```, causing the restart. But that should only happen if the language is changed using a spinner. The question is why it is assumed here that something has already changed when the fragment is called. – ng-User Mar 11 '22 at 09:02
  • If you changing language i dont think you have to use intent and to restart app.. that if you are using string and translation – Taa Lee Mar 11 '22 at 10:25
  • Yes, I use the value strings for translation. But I wouldn't know how else to do it if not with intent. – ng-User Mar 11 '22 at 10:41
  • https://stackoverflow.com/questions/49423451/how-to-change-the-whole-application-language – Taa Lee Mar 11 '22 at 11:33
  • @ng-User i updated my answer hope this working example help understand what you were doing wrong until you have the time to do a better research of your problem – Taa Lee Mar 11 '22 at 12:43
  • note this example is not perfect but to show you how to – Taa Lee Mar 11 '22 at 12:55