Android-侦听没有静态变量的语言环境更改 [英] Android - Listening to a locale change without static variables

查看:115
本文介绍了Android-侦听没有静态变量的语言环境更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BroadcastReceiver,它侦听区域设置更改.这是我的问题:

I have a BroadcastReceiver that listens to a locale change. Here's my problem:

我导航到Activity,然后想通过转到设置应用程序来更改语言环境(语言设置).进行更改后,BroadcastReceiver随后将监听onReceive().然后,我导航回到该应用程序,并在这样做时希望将用户带到另一个Activity.

I navigate to an Activity and then want to change locale (language setting) which I do by going to the settings app. The BroadcastReceiver then listens in onReceive() once a change is made. I then navigate back to the app and when I do so I'd like to take a user to another Activity.

此外,区域设置修改对应于配置更改,这意味着将销毁并重新创建活动. https://developer.android.com/guide/topics/resources/runtime -changes.html

Also, a locale modification corresponds to a change in configuration which means an Activity will be destroyed and created again. https://developer.android.com/guide/topics/resources/runtime-changes.html

这是BroadcastReceiver:

Here is the BroadcastReceiver:

public class LocaleReceiver extends BroadcastReceiver {
    public LocaleReceiver() {}

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.

        if(Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())){
            MainActivity.isLocaleChanged = true;
        }
    }
}

这是使用BroadcastReceiver设置的静态变量的Activity.

And here is the Activity that uses the static variable set by the BroadcastReceiver.

public class MainActivity extends AppCompatActivity {

    public static boolean isLocaleChanged = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(isLocaleChanged){
            Intent intent = new Intent(this,SecondActivity.class);
            startActivity(intent);
            isLocaleChanged = false;
        }
    }
}

事实上,我能够导航到其他活动!

And indeed I am able to navigate to a different Activity !

但是,我想以一种不使用静态变量的方式来执行此操作(因为它们是邪恶的:().还有其他方法可以完成此操作.

However, I'd like to do this in a manner that does not use static variables (since they are evil :(). Is there any other way to accomplish this.

如果不涉及SharedPreferences,我也会感到特别高兴.

I'd also be extra happy if there were no SharedPreferences involved.

推荐答案

好,您可以关闭区域设置的配置更改,实施onConfigurationChanged,检查更改是否针对区域设置,然后在此处启动新活动.我不确定是否会建议使用字符串返回时遇到问题.在这种情况下,您必须非本地地存储状态-要么以静态,磁盘(sharedPreference)方式存储,要么通过状态单例或其他某种方式存储.静电不是邪恶的,而是可以被滥用的.在这种情况下,它们很有意义.

Well, you can turn off configuration changes for locale, implement onConfigurationChanged, check if the change is to the locale, and launch the new activity there. I'm not sure if I'd suggest it though, you'll have issues when you return with strings. This is a case where you have to store state non-locally- either in a static, on disk (sharedPreference) or via a state singleton, or some other means. It isn't that statics are evil, its that they can be misused. This is a case where they make sense.

实际上,我会在这里建议使用静态优先于共享首选项,因为如果您未正确清除共享首选项,可能会滞后于共享首选项,从而导致以后执行应用程序更加麻烦.静态不会,当您的应用程序在最坏的情况下被杀死时,它将被清除.

I'd actually recommend static over shared preference here, as shared preferences may stick around if you don't clear it properly and screw up a later execution of your app. A static won't, it will be cleared when your app is killed worst case.

这篇关于Android-侦听没有静态变量的语言环境更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆