From the fragment (settings) by pressing the TextView
called DialogFragment
, in which to change the settings (language) application, how do I close by DialogFragment
, apply the settings without restarting the entire application?
Ideally to change languages at once on SingleChoice
selection dialog. With reboot everything works. Here is the code DialogFragment
:
public class LanguageDialogFragment extends DialogFragment {
final String[] itemsLang = {"English", "Russian", "Ukraine"};
final String[] items = {"en", "ru", "uk"};
String lang;
MainActivity activity = new MainActivity();
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Выберите свой родной язык")
.setSingleChoiceItems(itemsLang, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
lang = items[item];
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
App.prefs.saveLanguage(lang);
App.changeLang(lang);
reload();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
private void reload() {
Intent intent = getActivity().getIntent();
getActivity().finish();
Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(App.context.getPackageName());
startActivity(LaunchIntent);
}
}
Below are examples of the application as much as I wanted to implement: https://play.google.com/store/apps/details?id=com.funeasylearn.english
Screenshots from it:
Try the code below. You have to create a new locale based on the language the user selected, set it as the current locale and restart your activity. Be aware you might have to restart other activities if they are active when you do the locale switch.