You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio Java开发 切换语言导致App无限重启如何解决

问题原因

Android的Spinner控件在设置适配器完成后会自动触发一次onItemSelected回调,默认选中首个选项。你的代码未对触发场景做判断,只要回调触发就会执行语言切换和Activity重启逻辑,Activity重建后Fragment会重新初始化,再次触发Spinner的选中回调,最终形成无限重启循环。

解决方法

新增标记位过滤初始化自动触发的回调,同时对比选中语言和当前生效语言,仅当用户主动选择了不同语言时才执行重启操作。修改后的代码如下:

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    // 新增:标记是否是Spinner首次初始化的自动触发
    private boolean isSpinnerFirstLoad = true;
    // 新增:记录当前App生效的语言代码
    private String currentLangCode;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        Spinner spinner = root.findViewById(R.id.spinner);
        ImageView flags = (ImageView) root.findViewById(R.id.flag);

        // 初始化时先获取当前系统生效的语言代码
        currentLangCode = getResources().getConfiguration().locale.getLanguage();

        spinner.setAdapter(new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_spinner_dropdown_item,
                CountryData.countryNames));
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                flags.setImageResource(CountryData.countryFlag[spinner.getSelectedItemPosition()]);
                // 过滤首次初始化的自动触发回调
                if (isSpinnerFirstLoad) {
                    isSpinnerFirstLoad = false;
                    return;
                }
                String selectedLang = parent.getItemAtPosition(position).toString();
                String targetLangCode = selectedLang.equals("GR") ? "el" : "en";
                // 只有选中的语言和当前生效语言不同时才执行切换
                if (!targetLangCode.equals(currentLangCode)) {
                    setLocal(HomeFragment.this, targetLangCode);
                    // 更新当前生效语言代码
                    currentLangCode = targetLangCode;
                }
            }

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

            }
        });
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    public void setLocal(HomeFragment activity , String langCode){
      Locale locale = new Locale(langCode);
      locale.setDefault(locale);
      Resources resources = activity.getResources();
      Configuration config = resources.getConfiguration();
      config.setLocale(locale);
      resources.updateConfiguration(config,resources.getDisplayMetrics());
      getActivity().recreate();
    }
}
补充说明

如果后续需要扩展支持更多语言,只需要在targetLangCode的判断分支里新增对应语言的映射即可,核心逻辑不需要额外调整。

内容的提问来源于stack exchange,提问作者Βαρβάρα Ξιάρχου

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 09:45:00