1
從片段(設置)中通過按TextView
調用DialogFragment
,在其中更改設置(語言)應用程序,如何關閉DialogFragment
,應用設置而不重新啓動整個應用程序?Android在應用程序內切換語言而不重新啓動
理想情況下,在SingleChoice
選擇對話框上一次更改語言。重啓後一切正常。下面是代碼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);
}
}
下面是應用程序的例子,就像我想要實現:https://play.google.com/store/apps/details?id=com.funeasylearn.english
截圖從中:
不是一個完美的解決方案,應用類的靜態變量和JNI庫將不會重新初始化.. – blueware
我不想becasue程序是做這樣的記錄屏幕一些任務重新啓動我的應用程序。所以沒有重新啓動應用程序是有任何解決方案的Android 7.0 – PriyankaChauhan