简体   繁体   English

为什么在应用程序处于后台时更改设备语言后,SavedInstanceState 在活动中不是 null?

[英]Why SavedInstanceState is not null in activity after changing device language while the app is in background?

I created a new activity for my app and implemented a BroadcastReceiver to handle the device language changing and when the user put the app in background and change the language, the activity should be recreated.我为我的应用程序创建了一个新活动,并实现了一个 BroadcastReceiver 来处理设备语言更改,当用户将应用程序置于后台并更改语言时,应该重新创建活动。 I call this method in onCreate in my activity and I check if savedInstanceState is null before calling the languageReceiver(), but when I change the language, the savedInstanceState is not null and the method is not called...我在我的活动中的 onCreate 中调用此方法,并在调用 languageReceiver() 之前检查 savedInstanceState 是否为 null,但是当我更改语言时,savedInstanceState 不是 null 并且没有调用该方法...

The setupLangReceiver() method setupLangReceiver() 方法

private fun setupLangReceiver(): BroadcastReceiver? {
    if (mLangReceiver == null) {
        mLangReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                recreateActivity(intent)
            }
        }
        registerReceiver(mLangReceiver, IntentFilter(Intent.ACTION_LOCALE_CHANGED))
    }
    return mLangReceiver
}

The recreateActivity() method recreateActivity() 方法

 private fun recreateActivity(intent: Intent) {
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
    finish()
    overridePendingTransition(0, 0)
    startActivity(intent)
    overridePendingTransition(0, 0)
}

SetupLangReceiver method calling SetupLangReceiver 方法调用

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_prb_module)
    ThemeUtils.onActivityCreateSetTheme(this)

    if (savedInstanceState == null) {
        setupLangReceiver()
    }

After u change the system langugae, ur app can be recreate by the system.更改系统语言后,系统可以重新创建您的应用程序。 And the system saves the activity's sate automatically.系统自动保存活动状态。 So u can ovveride the method onSaveInstanceState所以你可以覆盖onSaveInstanceState方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回活动后,savedInstanceState为null - savedInstanceState null after returning to activity OS杀死活动后,片段savedInstanceState不为null - Fragment savedInstanceState not null after activity killed by OS 活动销毁后,savedInstanceState始终为null - savedInstanceState is always null after activity gets destroyed 设备轮换后,savedInstanceState 包始终为 null - savedInstanceState bundle is always null after device rotation 更改设备语言后,我的应用程序的语言未更改 - My app's language is not changing after changing the device language 在后台杀死应用程序时,捆绑的saveInstanceInstance可打包对象为null = null - Bundle savedInstanceState parcelable object is null=null when killing the app in background 为什么在重新创建活动时我的savedInstanceState为空? - Why is my savedInstanceState Null when recreating my activity? savedInstanceState 是 null 在第一次应用程序关闭后但不是在第二次 - savedInstanceState is null after first app shutdown but not after second 更改语言设置后 Android 应用程序中的空指针异常 - null pointer exception in Android app after changing language setting 更改活动或最小化应用程序后在后台运行的音频 - Audio running in background after changing the activity or minimizing the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM