[英]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.