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

Spinner实现多语言切换遇异常:重复创建页面及ProgressDialog不显示

修复Spinner多语言切换的两个问题

我来帮你逐一解决遇到的两个问题:

问题1:切换语言时重复创建MainActivity

原因分析

你的setLocale方法里同时执行了this.recreate()和startActivity(MainActivity),这会触发两个独立的页面操作:

  • 先重启当前的SettingActivity
  • 再启动新的MainActivity
    最终导致Activity栈中出现多个实例,造成重复创建的问题。

修复方案

只保留一种页面重启逻辑即可:如果你希望切换语言后回到主页面,就关闭当前设置页面并启动MainActivity(加上清除栈顶标记避免重复实例);如果想留在设置页面,就只调用recreate()。

修改后的setLocale方法:

public void setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    
    // 适配Android 7.0+的Locale设置方式
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        conf.setLocale(myLocale);
    } else {
        conf.locale = myLocale;
    }
    res.updateConfiguration(conf, dm);

    // 保存语言设置到SharedPreferences,避免重启APP后恢复默认
    SharedPreferences prefs = getSharedPreferences("AppSettings", MODE_PRIVATE);
    prefs.edit().putString("Language", lang).apply();

    // 方式1:切换语言后回到MainActivity,清除栈顶避免重复实例
    Intent refresh = new Intent(this, MainActivity.class);
    refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(refresh);
    finish(); // 关闭当前设置页面

    // 如果你想留在设置页面,就注释上面的Intent代码,改用下面这行:
    // this.recreate();
}

问题2:ProgressDialog无法正常显示

原因分析

你在onItemSelected里先调用了setLocale(里面马上触发页面重启),再调用progressDialog.show()。此时当前Activity已经进入销毁/后台状态,Dialog还没来得及渲染就被销毁了,自然无法显示。另外setLocale末尾的progressDialog.dismiss()也是多余的,因为Activity销毁后Dialog会自动消失。

修复方案

调整调用顺序,先显示Dialog,再执行语言切换逻辑;如果希望用户能清楚看到提示,可以延迟一小段时间再执行切换:

修改后的onItemSelected方法:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // 先显示Dialog
        progressDialog.show();
        
        // 延迟500毫秒执行语言切换,让Dialog有足够时间展示
        new Handler().postDelayed(() -> {
            if (pos == 1) {
                setLocale("in");
            } else if (pos == 2) {
                setLocale("en");
            }
        }, 500);
    }

    public void onNothingSelected(AdapterView<?> arg0) {
        // 空实现
    }
});

额外优化建议

为了保证APP重启后语言设置不会丢失,建议在BaseActivity或者Application的onCreate方法中,从SharedPreferences读取保存的语言并重新设置,比如:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 读取保存的语言设置
    SharedPreferences prefs = getSharedPreferences("AppSettings", MODE_PRIVATE);
    String savedLang = prefs.getString("Language", "en"); // 默认英文
    setLocale(savedLang);
}

内容的提问来源于stack exchange,提问作者asqa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:50:29