You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

应用语言切换不彻底问题求助:跳转Activity后语言复原

Android应用语言切换后跨Activity失效的解决方案

首先,我来梳理下你遇到的问题:

选择英文后主界面显示英文,但跳转至其他Activity时语言恢复为原始语言,返回主Activity后语言仍为原始语言;但再次点击英文或法文选项时无异常。

你当前使用的切换代码如下:

public void dilidegistir(String dil){ 
    Resources activityRes = getResources(); 
    Configuration activityConf = activityRes.getConfiguration(); 
    Locale newLocale = new Locale(dil); 
    activityConf.setLocale(newLocale); 
    activityRes.updateConfiguration(activityConf, activityRes.getDisplayMetrics()); 

    Resources applicationRes = getApplicationContext().getResources(); 
    Configuration applicationConf = applicationRes.getConfiguration(); 
    applicationConf.setLocale(newLocale); 
    applicationRes.updateConfiguration(applicationConf, applicationRes.getDisplayMetrics()); 

    finish(); 
    startActivity(getIntent()); 
}

问题根源

你的代码只临时更新了当前Activity和Application的Resources配置,但没有持久化语言设置,而且在Android 7.0(API 24)及以上版本,updateConfiguration方法已经被标记为过时,系统在创建新Activity时会重新加载默认配置(比如系统语言),导致之前的切换失效。再次点击切换时,当前Activity又被重新创建,临时配置生效,但跨Activity依然会丢失。

解决方案:持久化Locale + 全局配置生效

我给你一套完整的解决方案,确保语言切换在所有Activity中都能保持生效:

1. 编写Locale工具类,负责持久化和配置更新

创建一个LocaleHelper类,用来保存用户选择的语言,并提供更新Context配置的方法:

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.content.res.Configuration;
import java.util.Locale;

public class LocaleHelper {
    private static final String KEY_SELECTED_LANG = "selected_language";

    // 保存用户选择的语言到SharedPreferences
    public static void saveSelectedLanguage(Context context, String languageCode) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putString(KEY_SELECTED_LANG, languageCode).apply();
    }

    // 获取保存的语言,默认返回系统当前语言
    public static String getSelectedLanguage(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getString(KEY_SELECTED_LANG, Locale.getDefault().getLanguage());
    }

    // 更新Context的Locale配置,适配不同Android版本
    public static Context updateContextLocale(Context context, String languageCode) {
        Locale newLocale = new Locale(languageCode);
        Locale.setDefault(newLocale);

        Resources resources = context.getResources();
        Configuration config = resources.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            // Android 7.0+ 使用createConfigurationContext创建新的Context
            config.setLocale(newLocale);
            config.setLayoutDirection(newLocale);
            return context.createConfigurationContext(config);
        } else {
            // 低版本直接更新配置
            config.locale = newLocale;
            resources.updateConfiguration(config, resources.getDisplayMetrics());
            return context;
        }
    }
}

2. 修改你的语言切换方法,添加持久化逻辑

更新dilidegistir方法,先保存用户选择的语言,再更新当前Context配置:

public void dilidegistir(String dil){ 
    // 第一步:持久化用户选择的语言
    LocaleHelper.saveSelectedLanguage(this, dil);
    
    // 第二步:更新当前Activity的Context配置
    Context updatedContext = LocaleHelper.updateContextLocale(this, dil);
    Resources activityRes = updatedContext.getResources(); 
    Configuration activityConf = activityRes.getConfiguration(); 
    activityRes.updateConfiguration(activityConf, activityRes.getDisplayMetrics()); 

    // 重启当前Activity生效
    finish(); 
    startActivity(getIntent()); 
}

3. 全局应用配置:自定义Application类

创建一个自定义的Application类,在attachBaseContext中加载保存的语言配置,这样所有新创建的Activity都会继承这个配置:

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

public class MyApp extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        // 加载保存的语言,更新BaseContext
        String savedLang = LocaleHelper.getSelectedLanguage(base);
        super.attachBaseContext(LocaleHelper.updateContextLocale(base, savedLang));
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // 系统配置变更时(比如系统语言切换),重新应用我们保存的语言
        String savedLang = LocaleHelper.getSelectedLanguage(this);
        LocaleHelper.updateContextLocale(this, savedLang);
    }
}

然后在AndroidManifest.xml中注册这个Application:

<application
    android:name=".MyApp"
    <!-- 其他配置 -->
    >
    <!-- 你的Activity配置 -->
</application>

备选方案:使用基类Activity统一处理

如果不想修改Application类,你可以创建一个BaseActivity,让所有Activity都继承它,在attachBaseContext中应用语言配置:

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void attachBaseContext(Context newBase) {
        String savedLang = LocaleHelper.getSelectedLanguage(newBase);
        super.attachBaseContext(LocaleHelper.updateContextLocale(newBase, savedLang));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

验证效果

完成以上配置后,你再切换语言:

  1. 选择英文,主界面显示英文
  2. 跳转到其他Activity,语言会保持英文
  3. 返回主Activity,语言依然是英文
  4. 重启应用后,也会保持用户选择的语言

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

火山引擎 最新活动