如何在 Kotlin 中保存延迟发帖的状态
How to save the state of delayed posts in Kotlin
对于 Android 应用程序,我有一些内容想要延迟显示。因此我正在使用处理程序。
private lateinit var mHandler: Handler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
linearLayout.isVisible = false
mHandler = Handler()
mHandler.postDelayed({
linearLayout.isVisible = true
}, 1000)
}
如何保存此状态(例如在旋转应用程序的情况下)?使用 SharedPreferences
还是类似这样的东西更好:
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
}
我建议使用较新的 ViewModel
组件来解决此类问题。 ViewModel
不会像活动和片段那样在配置更改时被破坏,因此您可以 运行 您的 Handler
(或 Timer
)而不用担心丢失其状态。
class MainViewModel : ViewModel() {
private val layoutVisibility: MutableLiveData<Boolean> by lazy {
MutableLiveData().also {
delayVisibility()
}
}
private fun delayVisibility() {
Timer().schedule(1000) {
layoutVisibility.postValue(true)
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var model: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
model = ViewModelProviders.of(this)[MainViewModel::class.java]
linearLayout.isVisible = false
model.layoutVisibility.observe(this, Observer<Boolean> { visibility ->
linearLayout.isVisible = visibility == true
})
}
}
您 可以 保存处理程序的状态——通过保存开始时间并计算重新创建 activity 时经过的时间——但是 ViewModel
架构对我来说似乎更直观。
class MainActivity : AppCompatActivity() {
private lateinit var startTime: Long
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
linearLayout.isVisible = false
var elapsedTime: Long = 0L
if (savedInstanceState != null) {
startTime = savedInstanceState.getLong(KEY_START_TIME, System.currentTimeMillis())
elapsedTime = System.currentTimeMillis() - startTime
} else {
startTime = System.currentTimeMillis()
}
if (elapsedTime >= 1000) {
linearLayout.isVisible = true
} else {
Handler().postDelayed({
linearLayout.isVisible = true
}, 1000 - elapsedTime)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putLong(KEY_START_TIME, startTime)
}
companion object {
private const val KEY_START_TIME = "start_time"
}
}
对于 Android 应用程序,我有一些内容想要延迟显示。因此我正在使用处理程序。
private lateinit var mHandler: Handler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
linearLayout.isVisible = false
mHandler = Handler()
mHandler.postDelayed({
linearLayout.isVisible = true
}, 1000)
}
如何保存此状态(例如在旋转应用程序的情况下)?使用 SharedPreferences
还是类似这样的东西更好:
override fun onSaveInstanceState(outState: Bundle?) {
super.onSaveInstanceState(outState)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
super.onRestoreInstanceState(savedInstanceState)
}
我建议使用较新的 ViewModel
组件来解决此类问题。 ViewModel
不会像活动和片段那样在配置更改时被破坏,因此您可以 运行 您的 Handler
(或 Timer
)而不用担心丢失其状态。
class MainViewModel : ViewModel() {
private val layoutVisibility: MutableLiveData<Boolean> by lazy {
MutableLiveData().also {
delayVisibility()
}
}
private fun delayVisibility() {
Timer().schedule(1000) {
layoutVisibility.postValue(true)
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var model: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
model = ViewModelProviders.of(this)[MainViewModel::class.java]
linearLayout.isVisible = false
model.layoutVisibility.observe(this, Observer<Boolean> { visibility ->
linearLayout.isVisible = visibility == true
})
}
}
您 可以 保存处理程序的状态——通过保存开始时间并计算重新创建 activity 时经过的时间——但是 ViewModel
架构对我来说似乎更直观。
class MainActivity : AppCompatActivity() {
private lateinit var startTime: Long
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
linearLayout.isVisible = false
var elapsedTime: Long = 0L
if (savedInstanceState != null) {
startTime = savedInstanceState.getLong(KEY_START_TIME, System.currentTimeMillis())
elapsedTime = System.currentTimeMillis() - startTime
} else {
startTime = System.currentTimeMillis()
}
if (elapsedTime >= 1000) {
linearLayout.isVisible = true
} else {
Handler().postDelayed({
linearLayout.isVisible = true
}, 1000 - elapsedTime)
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putLong(KEY_START_TIME, startTime)
}
companion object {
private const val KEY_START_TIME = "start_time"
}
}