首页 > 解决方案 > Android 应用程序区域设置不会在应用程序启动时从共享首选项更改

问题描述

我的应用程序只需按一下按钮即可更改区域设置,这对于 Intent 刷新非常有用。& 一旦按下按钮,区域设置也会进入共享首选项。

我已经检查了 Locale 是否在应用程序开始时以正确的值正确保存和检索。

但是,一旦应用程序关闭并再次启动,我认为区域设置已更改,片段标题也已更改,但侧导航菜单标题仍保留在默认区域设置中,对我来说是英语。但是在 Fragments 内部,区域设置已正确更改。

这是我在 onCreate()、onStart() 上尝试过的代码,并且还包含在 MainActivity 扩展 AppCompatActivity 的 onResume() 中。

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

    hideSystemUI();

    sharedPref = getPreferences(Context.MODE_PRIVATE);
    selectedLanguage = sharedPref.getString("Test.SL.LanguageName", language);
    selectedTheme = sharedPref.getString("Test.SL.ThemeName", "Light");

    if (selectedTheme.equals("Light")){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else if (selectedTheme.equals("Dark")) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    language = selectedLanguage;

    Log.i("selectedlang", selectedLanguage);

    if (language.equals("සිංහල")) {
        setAppLocale(this, "si");
    } else if (language.equals("English")){
        setAppLocale(this, "en");
    }

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.appBarMain.toolbar);
    
    drawerLayout = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_a, R.id.nav_d, R.id.nav_a, R.id.nav_settings,
            R.id.nav_about, R.id.nav_disclaimer)
            .setOpenableLayout(drawerLayout)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);

    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

public void setAppLocale(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        hideSystemUI();
    }
}

private void hideSystemUI() {
SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

private void showSystemUI() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

请让我知道如何纠正我做错的事情。非常感谢你!

启动器活动作为简单的 UX 应用程序提到,但它似乎仍然无法正常工作,除非我再次在此特定代码中做错了什么。

public class Launcher extends AppCompatActivity {

String language = "English";


private SharedPreferences sharedPref;
private String selectedLanguage;
private String selectedTheme;

@Override
protected void onStart() {
    super.onStart();

    sharedPref = getPreferences(Context.MODE_PRIVATE);
    selectedLanguage = sharedPref.getString("Test.SL.LanguageName", language);
    selectedTheme = sharedPref.getString("Test.SL.ThemeName", "Light");

    if (selectedTheme.equals("Light")){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    } else if (selectedTheme.equals("Dark")) {
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }

    MainActivity.language = selectedLanguage;
    language = selectedLanguage;

    Log.i("selectedlang", selectedLanguage);

    if (language.equals("සිංහල")) {
        setAppLocale(this, "si");
    } else if (language.equals("English")){
        setAppLocale(this, "en");
    }
}

public void setAppLocale(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Configuration config = context.getResources().getConfiguration();
    config.setLocale(locale);
    context.createConfigurationContext(config);
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Intent refresh = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(refresh);
    finish();
}
}

标签: javaandroidandroid-studiolocale

解决方案


创建另一个活动作为启动器活动。

然后在清单中声明新的启动器活动,如下所示:

<activity android:name="com.sux.alarmclocknew.LauncherActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

将代码放在您在新 LauncherActivity 中设置区域设置的位置。设置区域设置后,从 LauncherActivity 启动 MainAcitivty。


推荐阅读